<!DOCTYPE html>
<html lang="en-US" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>脚本入门 | VitePress</title>
    <meta name="description" content="A VitePress site">
    <link rel="preload stylesheet" href="/notebook/assets/style.3dbfd0c2.css" as="style">
    
    <script type="module" src="/notebook/assets/app.8aaa4cbe.js"></script>
    <link rel="preload" href="/notebook/assets/inter-roman-latin.2ed14f66.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/notebook/assets/chunks/framework.1336c4e5.js">
    <link rel="modulepreload" href="/notebook/assets/chunks/theme.20cddc0c.js">
    <link rel="modulepreload" href="/notebook/assets/Linux_Shell.md.ae53d83b.lean.js">
    <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-255ec12d><!--[--><!--]--><!--[--><span tabindex="-1" data-v-ae3e3f51></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-ae3e3f51> Skip to content </a><!--]--><!----><header class="VPNav" data-v-255ec12d data-v-7e5bc4a5><div class="VPNavBar has-sidebar" data-v-7e5bc4a5 data-v-0937f67c><div class="container" data-v-0937f67c><div class="title" data-v-0937f67c><div class="VPNavBarTitle has-sidebar" data-v-0937f67c data-v-86d1bed8><a class="title" href="/notebook/" data-v-86d1bed8><!--[--><!--]--><!--[--><img class="VPImage logo" src="/notebook/Vue.png" alt data-v-8426fc1a><!--]--><!--[-->任硕的文档<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-0937f67c><div class="curtain" data-v-0937f67c></div><div class="content-body" data-v-0937f67c><!--[--><!--]--><div class="VPNavBarSearch search" style="--vp-meta-key:&#39;Meta&#39;;" data-v-0937f67c><!--[--><!----><div id="docsearch"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg class="DocSearch-Search-Icon" width="20" height="20" viewBox="0 0 20 20" aria-label="search icon"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-0937f67c data-v-7f418b0f><span id="main-nav-aria-label" class="visually-hidden" data-v-7f418b0f>Main Navigation</span><!--[--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>Java学前端</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/HTML+JS.html" data-v-2f2cfafc><!--[-->HTML+JS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/CSS.html" data-v-2f2cfafc><!--[-->CSS<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue2+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue2+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/Vue3+%E7%BB%84%E4%BB%B6.html" data-v-2f2cfafc><!--[-->Vue3+组件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/Java%E5%AD%A6%E5%89%8D%E7%AB%AF/React.html" data-v-2f2cfafc><!--[-->React<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>软件测试</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->测试基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E5%8E%8B%E5%8A%9B%E6%B5%8B%E8%AF%95.html" data-v-2f2cfafc><!--[-->压力测试<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>多线程</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-2f2cfafc><!--[-->基础篇<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E5%B9%B6%E5%8F%91%20&amp;%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/%E5%B9%B6%E5%8F%91%E5%AE%8C%E5%96%84.html" data-v-2f2cfafc><!--[-->进阶篇<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>开发工具</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/Chrome.html" data-v-2f2cfafc><!--[-->Chrome<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E5%9F%BA%E7%A1%80.html" data-v-2f2cfafc><!--[-->IDEA基础<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/IDEA%E6%8F%92%E4%BB%B6.html" data-v-2f2cfafc><!--[-->IDEA插件<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/IDEA/VS%20Code.html" data-v-2f2cfafc><!--[-->VS Code<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-7f418b0f data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-a7b5672a><span class="text" data-v-a7b5672a><!----><span data-v-a7b5672a>消息中间件</span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-a7b5672a><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuGroup" data-v-e7ea1737 data-v-69e747b5><!----><!--[--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RabbitMQ.html" data-v-2f2cfafc><!--[-->RabbitMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/RocketMQ.html" data-v-2f2cfafc><!--[-->RocketMQ<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Kafka.html" data-v-2f2cfafc><!--[-->Kafka<!--]--></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-69e747b5 data-v-2f2cfafc><a class="VPLink link" href="/notebook/%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6/Canal.html" data-v-2f2cfafc><!--[-->Canal<!--]--></a></div><!--]--><!--]--></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-0937f67c data-v-f6a63727><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-f6a63727 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-0937f67c data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-0937f67c data-v-40855f84 data-v-a7b5672a><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-a7b5672a><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-a7b5672a><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-a7b5672a><div class="VPMenu" data-v-a7b5672a data-v-e7ea1737><!----><!--[--><!--[--><!----><div class="group" data-v-40855f84><div class="item appearance" data-v-40855f84><p class="label" data-v-40855f84>Appearance</p><div class="appearance-action" data-v-40855f84><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title="toggle dark mode" aria-checked="false" data-v-40855f84 data-v-82b282f1 data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-82b282f1><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-82b282f1><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><div class="group" data-v-40855f84><div class="item social-links" data-v-40855f84><div class="VPSocialLinks social-links-list" data-v-40855f84 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/renshuo123/renshuo123.github.io" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><a class="VPSocialLink no-icon" href="#" aria-label="twitter" target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg></a><a class="VPSocialLink no-icon" href="https://github.com/" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-f80f8133><svg t="1676028692954" class="icon" ...</path></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-0937f67c data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav reached-top" data-v-255ec12d data-v-5cfd5582><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-5cfd5582><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-5cfd5582><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-5cfd5582>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-5cfd5582 data-v-18201f51><button data-v-18201f51>Return to top</button><!----></div></div><aside class="VPSidebar" data-v-255ec12d data-v-845b8fc6><div class="curtain" data-v-845b8fc6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-845b8fc6><span class="visually-hidden" id="sidebar-aria-label" data-v-845b8fc6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Java</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%9B%86%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java集合</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Java/Java%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Java高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible has-active" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Linux</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Linux%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Linux新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/Shell.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Shell脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实用脚本</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Linux/%E8%BD%AF%E4%BB%B6%E9%83%A8%E7%BD%B2.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>软件部署</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Nginx</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E5%AE%9E%E6%88%98%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>实战篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Nginx/%E9%9D%A2%E8%AF%95%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>面试篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SSM</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Maven.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Maven</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/Spring.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Spring</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringMVC.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringMVC</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/SSM/SpringBatch.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringBatch</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringBoot</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E5%BA%94%E7%94%A8%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>应用篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E6%96%B0%E7%89%B9%E6%80%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>新特性</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/3%E3%80%81SpringBoot/%E8%BF%90%E7%BB%B4&amp;%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>运维&原理</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringCloud</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringCloud</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/Sentinel.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Sentinel</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>SpringSecurity</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/SpringSecurity/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>SpringSecurity高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Mybatis & MybatisPlus</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/Mybatis.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Mybatis</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/MybatisPlus.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MybatisPlus</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/Mybatis&amp;MybatisPlus/JPA.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JPA</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>Git & ChatGPT</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Git.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Git</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Github.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Github</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/ChatGPT.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ChatGPT</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Jenkins.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Jenkins</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/5%E3%80%81%E8%BF%90%E7%BB%B4/Netty.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Netty</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>数据库</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MySQL</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%AE%BE%E8%AE%A1.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL设计</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E6%A0%B8%E5%BF%83/%E8%BF%90%E7%BB%B4.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MySQL运维</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分库分表</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Redis</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%8E%9F%E7%90%86.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis原理</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/Redis%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Redis实战</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Redis/%E6%9C%AC%E5%9C%B0%E7%BC%93%E5%AD%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>本地缓存</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>MongoDB</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/MongoDB/%E6%95%B4%E5%90%88.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>MongoDB进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>ElasticSearch</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/1%E3%80%81ES%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch/3%E3%80%81ES%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES高级</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/influxdb.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>InfluxDB</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/2%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/Neo4j.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Neo4j</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>高并发 & 秒杀 & 分布式</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E5%88%86%E5%B8%83%E5%BC%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式理论</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/4%E3%80%81%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BF%85%E5%A4%87/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>分布式锁</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E7%A7%92%E6%9D%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>秒杀</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%8F%AF%E7%94%A8.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高可用</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%B8%89%E9%AB%98/%E9%AB%98%E5%B9%B6%E5%8F%91.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高并发</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>云原生</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/Docker.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Docker</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E4%BA%91%E5%8E%9F%E7%94%9F/K8S.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>K8S</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>可视化 & 监控</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E7%9B%91%E6%8E%A7%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>监控进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%A7%E5%B1%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>可视化大屏</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E5%8F%AF%E8%A7%86%E5%8C%96%20&amp;%20%E7%9B%91%E6%8E%A7/Zabbix.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Zabbix</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>学前端</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>HTML+CSS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/HTML%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>HTML基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/CSS%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>CSS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/1%E3%80%81HTML+CSS/%E7%BD%91%E9%A1%B5%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>网页进阶</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>JS+TS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/JS%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>JS进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/ES6%20%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>ES6进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/2%E3%80%81JS+TS/TypeScript.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>TS基础</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>NodeJS</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Node进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/4%E3%80%81Node/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>Vue</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E8%BF%9B%E9%98%B6.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3进阶</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E9%AB%98%E7%BA%A7.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3高级</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue3/Vue3%E6%96%B0%E8%AF%AD%E6%B3%95.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>Vue3新语法</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/3%E3%80%81Vue/Vue2/Vue2%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小程序</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序基础</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BC%98%E5%8C%96.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>小程序优化</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/uniapp.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>uniapp</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/1%E3%80%81%E5%AD%A6%E5%89%8D%E7%AB%AF/5%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E9%A1%B9%E7%9B%AE.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目实战</p><!--]--></a><!----></div><!----></div><!--]--></div></section><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>计算机基础</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>数据结构</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>操作系统</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>设计模式</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>计算机网络</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/UML.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>UML</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E7%AE%97%E6%B3%95/LeetCode.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>LeetCode</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0 collapsible collapsed" data-v-845b8fc6 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h2 class="text" data-v-9b797284>项目实战</h2><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>云尚办公</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E4%BA%91%E5%B0%9A%E5%8A%9E%E5%85%AC/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>小兔鲜</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%871.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇1</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E5%B0%8F%E5%85%94%E9%B2%9C/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>地图</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E7%99%BE%E5%BA%A6%E5%9C%B0%E5%9B%BE/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>苍穹外卖</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E8%8B%8D%E7%A9%B9%E5%A4%96%E5%8D%96/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><section class="VPSidebarItem level-1 collapsible collapsed" data-v-9b797284 data-v-9b797284><div class="item" role="button" tabindex="0" data-v-9b797284><div class="indicator" data-v-9b797284></div><h3 class="text" data-v-9b797284>黑马头条</h3><div class="caret" role="button" aria-label="toggle section" tabindex="0" data-v-9b797284><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-9b797284><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E5%9F%BA%E7%A1%80%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>基础篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E8%BF%9B%E9%98%B6%E7%AF%872.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>进阶篇2</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-2 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%BB%91%E9%A9%AC%E5%A4%B4%E6%9D%A1/%E9%AB%98%E7%BA%A7%E7%AF%87.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>高级篇</p><!--]--></a><!----></div><!----></div><!--]--></div></section><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E6%94%AF%E4%BB%98.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>支付</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>项目推荐</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-845b8fc6><section class="VPSidebarItem level-0" data-v-845b8fc6 data-v-9b797284><!----><div class="items" data-v-9b797284><!--[--><div class="VPSidebarItem level-1 is-link" data-v-9b797284 data-v-9b797284><div class="item" data-v-9b797284><div class="indicator" data-v-9b797284></div><a class="VPLink link link" href="/notebook/team.html" data-v-9b797284><!--[--><p class="text" data-v-9b797284>团队成员</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-255ec12d data-v-669faec9><div class="VPDoc has-sidebar has-aside" data-v-669faec9 data-v-6b87e69f><!--[--><!--]--><div class="container" data-v-6b87e69f><div class="aside" data-v-6b87e69f><div class="aside-curtain" data-v-6b87e69f></div><div class="aside-container" data-v-6b87e69f><div class="aside-content" data-v-6b87e69f><div class="VPDocAside" data-v-6b87e69f data-v-3f215769><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" data-v-3f215769 data-v-ff0f39c8><div class="content" data-v-ff0f39c8><div class="outline-marker" data-v-ff0f39c8></div><div class="outline-title" data-v-ff0f39c8>On this page</div><nav aria-labelledby="doc-outline-aria-label" data-v-ff0f39c8><span class="visually-hidden" id="doc-outline-aria-label" data-v-ff0f39c8> Table of Contents for current page </span><ul class="root" data-v-ff0f39c8 data-v-d0ee3533><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-6b87e69f><div class="content-container" data-v-6b87e69f><!--[--><!--]--><!----><main class="main" data-v-6b87e69f><div style="position:relative;" class="vp-doc _notebook_Linux_Shell" data-v-6b87e69f><div><h1 id="脚本入门" tabindex="-1">脚本入门 <a class="header-anchor" href="#脚本入门" aria-label="Permalink to &quot;脚本入门&quot;">​</a></h1><h2 id="什么是shell" tabindex="-1">什么是Shell <a class="header-anchor" href="#什么是shell" aria-label="Permalink to &quot;什么是Shell&quot;">​</a></h2><p>通过编写Shell命令发送给linux内核去执行, 操作就是计算机硬件. 所以Shell命令是用户操作计算机硬件的桥梁,</p><p>Shell是命令, 类似于windows系统Dos命令</p><p>Shell是一个门程序设计语言, Shell里面含有变量, 函数, 逻辑控制语句等等</p><h2 id="shell的运行过程" tabindex="-1">Shell的运行过程 <a class="header-anchor" href="#shell的运行过程" aria-label="Permalink to &quot;Shell的运行过程&quot;">​</a></h2><p>当用户下达指令给该操作系统的时候，实际上是把指令告诉shell，经过shell解释，处理后让内核做出相应的动作。 系统的回应和输出的信息也由shell处理，然后显示在用户的屏幕上。</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042118727.png" alt="image-20200313223530826" style="zoom:80%;"><h2 id="shell解析器" tabindex="-1">Shell解析器 <a class="header-anchor" href="#shell解析器" aria-label="Permalink to &quot;Shell解析器&quot;">​</a></h2><p>查看linux系统centos支持的shell解析器</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cat</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/shells</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202208051112448.png" alt="image-20220805101538710" style="zoom:67%;"><p>介绍解析器类型</p><table><thead><tr><th>解析器类型</th><th>介绍</th></tr></thead><tbody><tr><td>/bin/sh</td><td>Bourne Shell,是UNIX最初使用的shell;</td></tr><tr><td><!----></td><td><!----></td></tr><tr><td>/sbin/nologin</td><td>未登录解析器, shell设置为/sbin/nologin 是用于控制用户禁止登陆系统的, 有时候有些服务，比如邮件服务，大部分都是用来接收主机的邮件而已，并不需要登陆</td></tr><tr><td>/bin/dash</td><td>dash（Debian Almquist Shell），也是一种 Unix shell。它比 Bash 小，只需要较少的磁盘空间，但是它的对话性功能也较少，交互性较差。</td></tr><tr><td>/bin/csh</td><td>C Shell是C语言风格Shell</td></tr><tr><td>/bin/tcsh</td><td>是C Shell的一个扩展版本。</td></tr></tbody></table><blockquote><p>Centos默认的解析器是bash</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># /bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $SHELL</span></span></code></pre></div><blockquote><p>含义: 打印输出当前系统环境使用的Shell解析器类型</p><p>echo 用于打印输出数据到终端</p><p>$SHELL是全局共享的读取解析器类型环境变量, 全局环境变量时所有的Shell程序都可以读取的变量,</p></blockquote><h2 id="编写规范⭐⭐" tabindex="-1">编写规范⭐⭐ <a class="header-anchor" href="#编写规范⭐⭐" aria-label="Permalink to &quot;编写规范⭐⭐&quot;">​</a></h2><h3 id="后缀名规范" tabindex="-1">后缀名规范 <a class="header-anchor" href="#后缀名规范" aria-label="Permalink to &quot;后缀名规范&quot;">​</a></h3><blockquote><p>shell脚本文件就是一个文本文件, 后缀名建议使用 <code>.sh</code> 结尾</p></blockquote><h3 id="首行格式规范" tabindex="-1">首行格式规范 <a class="header-anchor" href="#首行格式规范" aria-label="Permalink to &quot;首行格式规范&quot;">​</a></h3><p>首行需要设置Shell解析器的类型, 语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span></code></pre></div><blockquote><p>含义: 设置当前shell脚本文件采用bash解析器运行脚本代码</p></blockquote><h2 id="注释格式" tabindex="-1">注释格式 <a class="header-anchor" href="#注释格式" aria-label="Permalink to &quot;注释格式&quot;">​</a></h2><p>单行注释, 语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 注释内容</span></span></code></pre></div><p>多行注释, 语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">:</span><span style="color:#A6ACCD;">&lt;&lt;!</span></span>
<span class="line"><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">注释内容1</span></span>
<span class="line"><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">注释内容2</span></span>
<span class="line"><span style="color:#89DDFF;">!</span></span></code></pre></div><h2 id="helloword" tabindex="-1">HelloWord <a class="header-anchor" href="#helloword" aria-label="Permalink to &quot;HelloWord&quot;">​</a></h2><p>1、创建shell脚本文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">touch</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">helloworld.sh</span></span></code></pre></div><p>2、编辑文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">helloworld.sh</span></span></code></pre></div><p>3、增加shell脚本文件内容如下，并保存退出</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello world</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><p>4、执行脚本</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sh</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">helloworld.sh</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241003719.png" alt="image-20220924100320887" style="zoom:80%;"><h2 id="执行脚本3种方式⭐" tabindex="-1">执行脚本3种方式⭐ <a class="header-anchor" href="#执行脚本3种方式⭐" aria-label="Permalink to &quot;执行脚本3种方式⭐&quot;">​</a></h2><h3 id="_3种方式的区别" tabindex="-1">3种方式的区别 <a class="header-anchor" href="#_3种方式的区别" aria-label="Permalink to &quot;3种方式的区别&quot;">​</a></h3><blockquote><p>sh或bash执行脚本文件方式是<strong>直接使用Shell解析器运行脚本文件, 不需要可执行权限</strong></p><p>仅路径方式是执行脚本文件自己, 需要可执行权限</p></blockquote><h3 id="sh命令执行" tabindex="-1">sh命令执行 <a class="header-anchor" href="#sh命令执行" aria-label="Permalink to &quot;sh命令执行&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 相对路径</span></span>
<span class="line"><span style="color:#FFCB6B;">sh</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">helloworld.sh</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 绝对路径</span></span>
<span class="line"><span style="color:#FFCB6B;">sh</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/helloworld.sh</span></span></code></pre></div><h3 id="bash命令执行" tabindex="-1">bash命令执行 <a class="header-anchor" href="#bash命令执行" aria-label="Permalink to &quot;bash命令执行&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 相对路径效果</span></span>
<span class="line"><span style="color:#FFCB6B;">bash</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">helloworld.sh</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 绝对路径效果</span></span>
<span class="line"><span style="color:#FFCB6B;">bash</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/shell/helloworld.sh</span></span></code></pre></div><h3 id="仅路径执行" tabindex="-1">仅路径执行 <a class="header-anchor" href="#仅路径执行" aria-label="Permalink to &quot;仅路径执行&quot;">​</a></h3><p>步骤1：设置所有用户对此脚本文件增加可执行性权限</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># &quot;a+x&quot;是权限参数，它告诉计算机要将&quot;执行&quot;权限（&quot;x&quot;）添加给&quot;所有人&quot;（&quot;a&quot;）即赋予所有人可以执行的权限</span></span>
<span class="line"><span style="color:#FFCB6B;">chmod</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">a+x</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">helloworld.sh</span></span></code></pre></div><p>步骤2：执行脚本</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 相对路径执行命令</span></span>
<span class="line"><span style="color:#FFCB6B;">./helloworld.sh</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 绝对路径执行命令</span></span>
<span class="line"><span style="color:#FFCB6B;">/root/helloworld.sh</span></span></code></pre></div><h2 id="实战演练⭐" tabindex="-1">实战演练⭐ <a class="header-anchor" href="#实战演练⭐" aria-label="Permalink to &quot;实战演练⭐&quot;">​</a></h2><blockquote><p>实现在/root/itheima/目录下创建一个one.txt,在one.txt文件中增加内容“Hello Shell”。</p></blockquote><h3 id="实现步骤" tabindex="-1">实现步骤 <a class="header-anchor" href="#实现步骤" aria-label="Permalink to &quot;实现步骤&quot;">​</a></h3><p>1、进入root目录，执行创建/root/itheima目录命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">mkdir</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/itheima</span></span>
<span class="line"><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">/root/itheima</span></span></code></pre></div><p>2、创建/root/batch.sh文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">touch</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">batch.sh</span></span></code></pre></div><p>2、编辑batch.sh文件，编写shell命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">batch.sh</span></span></code></pre></div><p>3、编写命令</p><p>命令1：创建/root/itheima/one.txt文件</p><p>命令2：输出“I love Shell”字符串数据到one.txt文件中</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#FFCB6B;">touch</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/itheima/one.txt</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># 创建文件one.txt</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Hello Shell</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#C3E88D;">/root/itheima/one.txt</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;">#输出数据到one.txt文件中</span></span></code></pre></div><h3 id="运行脚本效果" tabindex="-1">运行脚本效果 <a class="header-anchor" href="#运行脚本效果" aria-label="Permalink to &quot;运行脚本效果&quot;">​</a></h3><p>运行batch.sh脚本文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sh</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">batch.sh</span></span></code></pre></div><p>查看one.txt文件内容</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cat</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">itheima/one.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241021067.png" alt="image-20220924102130973" style="zoom:80%;"><h1 id="变量" tabindex="-1">变量 <a class="header-anchor" href="#变量" aria-label="Permalink to &quot;变量&quot;">​</a></h1><ol><li>系统环境变量，是系统提供的共享变量.是linux系统加载Shell的配置文件中定义的变量共享给所有的Shell程序使用</li><li>自定义变量</li><li>特殊符号变量</li></ol><h2 id="系统变量" tabindex="-1">系统变量 <a class="header-anchor" href="#系统变量" aria-label="Permalink to &quot;系统变量&quot;">​</a></h2><h3 id="配置文件" tabindex="-1">配置文件 <a class="header-anchor" href="#配置文件" aria-label="Permalink to &quot;配置文件&quot;">​</a></h3><p>1.全局配置文件</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">/etc/profile</span></span>
<span class="line"><span style="color:#A6ACCD;">/etc/profile.d/*.sh</span></span>
<span class="line"><span style="color:#A6ACCD;">/etc/bashrc</span></span></code></pre></div><p>2.个人配置文件</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">当前用户/.bash_profile</span></span>
<span class="line"><span style="color:#A6ACCD;">当前用户/.bashrc</span></span></code></pre></div><p>一般情况下，我们都是直接针对全局配置进行操作。</p><h3 id="环境变量分类" tabindex="-1">环境变量分类 <a class="header-anchor" href="#环境变量分类" aria-label="Permalink to &quot;环境变量分类&quot;">​</a></h3><p>在Linux系统中，环境变量按照其作用范围不同大致可以分为系统级环境变量和用户级环境变量。</p><blockquote><p>系统级环境变量：Shell环境加载全局配置文件中的变量共享给所有用户所有Shell程序使用, 全局共享 用户级环境变量：Shell环境加载个人配置文件中的变量共享给当前用户的Shell程序使用, 登录用户使用</p></blockquote><h3 id="查看当前系统变量" tabindex="-1">查看当前系统变量 <a class="header-anchor" href="#查看当前系统变量" aria-label="Permalink to &quot;查看当前系统变量&quot;">​</a></h3><p>查看命令</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">env</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241024957.png" alt="image-20220924102420718" style="zoom:80%;"><h3 id="查看系统变量-自定变量-函数" tabindex="-1">查看系统变量+自定变量+函数 <a class="header-anchor" href="#查看系统变量-自定变量-函数" aria-label="Permalink to &quot;查看系统变量+自定变量+函数&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">set</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241027607.png" alt="image-20220924102759476" style="zoom:80%;"><h3 id="常用系统变量⭐" tabindex="-1">常用系统变量⭐ <a class="header-anchor" href="#常用系统变量⭐" aria-label="Permalink to &quot;常用系统变量⭐&quot;">​</a></h3><table><thead><tr><th>变量名称</th><th>含义</th></tr></thead><tbody><tr><td>PATH</td><td>与windows环境变量PATH功能一样，设置命令的搜索路径，以冒号为分割</td></tr><tr><td>HOME</td><td>当前用户主目录：/root</td></tr><tr><td>SHELL</td><td>当前shell解析器类型：/bin/bash</td></tr><tr><td>HISTFILE</td><td>显示当前用户执行命令的历史列表文件：/root/.bash_history</td></tr><tr><td>PWD</td><td>显示当前所在路径：/root</td></tr><tr><td>OLDPWD</td><td>显示之前的路径</td></tr><tr><td>HOSTNAME</td><td>显示当前主机名：itheima</td></tr><tr><td>HOSTTYPE</td><td>显示主机的架构，是i386、i686、还是x86、x64等：x86_64</td></tr><tr><td>LANG</td><td>设置当前系统语言环境：zh_CN.UTF-8</td></tr></tbody></table><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241026220.png" alt="image-20220924102658109" style="zoom:80%;"><h3 id="全局环境变量配置⭐" tabindex="-1">全局环境变量配置⭐ <a class="header-anchor" href="#全局环境变量配置⭐" aria-label="Permalink to &quot;全局环境变量配置⭐&quot;">​</a></h3><blockquote><p>当前用户进入Shell环境初始化的时候会加载全局配置文件/etc/profile里面的环境变量, 供给所有Shell程序使用</p><p>以后只要是所有Shell程序或命令使用的变量, 就可以定义在这个文件中</p></blockquote><blockquote><p>增加命令: 定义变量VAR1=VAR1 并导出为环境变量</p><p>扩展: <strong>vim里面的命令模式使用G快速定位到文件末尾位置, 使用gg定位到文件首行位置</strong></p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/profile</span></span></code></pre></div><p>添加设置变量VAR1=VAR1并导出成为环境变量, 在/etc/profile文件末尾添加如下命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 创建环境变量</span></span>
<span class="line"><span style="color:#A6ACCD;">name</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">zhangsan</span></span>
<span class="line"><span style="color:#C792EA;">export</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">name</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241219661.png" alt="image-20220924121918555" style="zoom:80%;"><p>3、:wq保存/etc/profile退出</p><p>4、重新加载/etc/profile文件数据更新系统环境变量</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">source</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/profile</span></span></code></pre></div><blockquote><p>注意：如果这一步不执行，无法读取更新的环境变量</p></blockquote><p>5、输出环境变量VAR1</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $name</span></span></code></pre></div><h2 id="自定义变量" tabindex="-1">自定义变量 <a class="header-anchor" href="#自定义变量" aria-label="Permalink to &quot;自定义变量&quot;">​</a></h2><h3 id="自定义局部变量" tabindex="-1">自定义局部变量 <a class="header-anchor" href="#自定义局部变量" aria-label="Permalink to &quot;自定义局部变量&quot;">​</a></h3><blockquote><p>就是定义在一个脚本文件中的变量, 只能在这个脚本文件中使用的变量, 就是局部变量</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">var_name</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">value</span></span></code></pre></div><blockquote><ol><li><strong>变量名称可以有字母,数字和下划线组成, 但是不能以数字开头</strong></li><li><strong>等号两侧不能有空格</strong></li><li><strong>在bash环境中, 变量的默认类型都是字符串类型, 无法直接进行数值运算</strong></li><li><strong>变量的值如果有空格, 必须使用双引号括起来</strong></li><li><strong>不能使用Shell的关键字作为变量名称</strong></li></ol></blockquote><h4 id="查询-删除" tabindex="-1">查询 &amp; 删除 <a class="header-anchor" href="#查询-删除" aria-label="Permalink to &quot;查询 &amp; 删除&quot;">​</a></h4><div class="language-Shell"><button title="Copy Code" class="copy"></button><span class="lang">Shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 语法1: 直接使用变量名查询</span></span>
<span class="line"><span style="color:#A6ACCD;">$var_name</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 语法2: 使用花括号,# 区别: 花括号方式适合拼接字符串</span></span>
<span class="line"><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">var_name</span><span style="color:#89DDFF;">}</span></span></code></pre></div><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">unset</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">var_name</span></span></code></pre></div><h4 id="实战演示⭐" tabindex="-1">实战演示⭐ <a class="header-anchor" href="#实战演示⭐" aria-label="Permalink to &quot;实战演示⭐&quot;">​</a></h4><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">name</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">renshuo</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 结论:  推荐大家使用花括号才是编程好习惯</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">my</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">is</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">name</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">my</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">is</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">name</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">123</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">my</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">is</span><span style="color:#A6ACCD;"> $name</span></span>
<span class="line"><span style="color:#82AAFF;">unset</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">name</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">my</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">is</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">name</span><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241032287.png" alt="image-20220924103235187" style="zoom:80%;"><h3 id="自定义常量" tabindex="-1">自定义常量 <a class="header-anchor" href="#自定义常量" aria-label="Permalink to &quot;自定义常量&quot;">​</a></h3><blockquote><p>就是变量设置值以后不可以修改的变量叫常量, 也叫只读变量，加上readonly即可</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">readonly var_name</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241035208.png" alt="image-20220924103528101" style="zoom:80%;"><h3 id="自定义全局变量" tabindex="-1">自定义全局变量 <a class="header-anchor" href="#自定义全局变量" aria-label="Permalink to &quot;自定义全局变量&quot;">​</a></h3><blockquote><p>就是在当前脚本文件中定义全局变量, 这个全局变量可以在当前Shell环境与子Shell环境中都可以使用</p></blockquote><h4 id="父子环境" tabindex="-1">父子环境 <a class="header-anchor" href="#父子环境" aria-label="Permalink to &quot;父子环境&quot;">​</a></h4><blockquote><p>例如: 有2个Shell脚本文件 A.sh 和 B.sh，如果 在A.sh脚本文件中执行了B.sh脚本文件, 那么A.sh就是父Shell环境, B.sh就是子Shell环境</p></blockquote><h4 id="自定义全局变量语法" tabindex="-1">自定义全局变量语法 <a class="header-anchor" href="#自定义全局变量语法" aria-label="Permalink to &quot;自定义全局变量语法&quot;">​</a></h4><div class="language-Shell"><button title="Copy Code" class="copy"></button><span class="lang">Shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">export</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">var_name1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">var_name2</span></span></code></pre></div><h4 id="案例需求" tabindex="-1">案例需求 <a class="header-anchor" href="#案例需求" aria-label="Permalink to &quot;案例需求&quot;">​</a></h4><blockquote><p>测试全局变量在子Shell中是否可用, 在父Shell中是否可用</p></blockquote><h4 id="实战演练" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练" aria-label="Permalink to &quot;实战演练&quot;">​</a></h4><p>创建demo2.sh和demo3.sh文件</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">touch</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">demo2.sh</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">demo3.sh</span></span></code></pre></div><p>编辑demo2.sh, 里面定义变量VAR4并设置为全局, 并里面执行demo3.sh脚本文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">demo2.sh</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#A6ACCD;">name1</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">itheima</span></span>
<span class="line"><span style="color:#C792EA;">export</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">name1</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">demo2.sh输出name1变量：</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">name1</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"><span style="color:#FFCB6B;">bash</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">demo3.sh</span></span></code></pre></div><p>编辑demo3.sh, 里面打印VAR4</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">demo3.sh</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">在demo3中打印name1变量：</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">name1</span><span style="color:#89DDFF;">}&quot;</span></span></code></pre></div><p>执行脚本文件demo2.sh, 观察打印name1效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303111359820.png" alt="image-20230311135951646" style="zoom:80%;"><p>执行脚本文件后, 在交互式Shell环境打印VAR4, 观察打印VAR4效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042118615.png" alt="image-20200608224459074"></p><h2 id="特殊变量⭐" tabindex="-1">特殊变量⭐ <a class="header-anchor" href="#特殊变量⭐" aria-label="Permalink to &quot;特殊变量⭐&quot;">​</a></h2><h3 id="获取输入参数-n" tabindex="-1">获取输入参数：$n <a class="header-anchor" href="#获取输入参数-n" aria-label="Permalink to &quot;获取输入参数：$n&quot;">​</a></h3><h4 id="语法规则" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则" aria-label="Permalink to &quot;语法规则&quot;">​</a></h4><blockquote><p><strong>用于接收脚本文件执行时传入的参数</strong></p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">$n</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 执行脚本文件传入参数语法</span></span>
<span class="line"><span style="color:#FFCB6B;">sh</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">脚本文件</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">输入参数1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">输入参数2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">...</span></span></code></pre></div><blockquote><ul><li>$0 用于获取当前脚本文件名称的</li><li>$1~$9, 代表获取第一输入参数到第9个输入参数</li><li>第10个以上的输入参数获取参数的格式: ${数字}, 否则无法获取</li></ul></blockquote><h4 id="案例演示" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示" aria-label="Permalink to &quot;案例演示&quot;">​</a></h4><p>创建demo4.sh文件</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">touch demo4</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">sh</span></span>
<span class="line"><span style="color:#A6ACCD;">vim demo4</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">sh</span></span></code></pre></div><p>编辑demo4.sh文件, 输出脚本文件名称\第一个输入参数\第二个输入参数</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">脚本文件名：</span><span style="color:#A6ACCD;font-style:italic;">${0}</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第一个输入参数：</span><span style="color:#A6ACCD;font-style:italic;">${1}</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第二个输入参数：</span><span style="color:#A6ACCD;font-style:italic;">${2}</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><p>执行demo4.sh文件,输入输出参数itcast itheima的2个输入参数, 观察效果</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh demo4</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">sh itcast heima</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241044971.png" alt="image-20220924104416872" style="zoom:80%;"><h3 id="获取输入参数个数" tabindex="-1">获取输入参数个数：$# <a class="header-anchor" href="#获取输入参数个数" aria-label="Permalink to &quot;获取输入参数个数：$#&quot;">​</a></h3><h4 id="语法规则-1" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-1" aria-label="Permalink to &quot;语法规则&quot;">​</a></h4><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 获取所有输入参数的个数</span></span>
<span class="line"><span style="color:#A6ACCD;">$#</span></span></code></pre></div><h4 id="案例演示-1" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-1" aria-label="Permalink to &quot;案例演示&quot;">​</a></h4><p>编辑demo4.sh, 输出输入参数个数</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">vim demo4</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">sh</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">脚本文件名：</span><span style="color:#A6ACCD;font-style:italic;">${0}</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第一个输入参数：</span><span style="color:#A6ACCD;font-style:italic;">${1}</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第二个输入参数：</span><span style="color:#A6ACCD;font-style:italic;">${2}</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">输入参数个数：</span><span style="color:#A6ACCD;">$#</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><p>执行demo4.sh传入参数看效果</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh demo4</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">sh itcast heima renshuo</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241051936.png" alt="image-20220924105109806" style="zoom:80%;"><h3 id="获取所有输入参数-、-⭐" tabindex="-1">获取所有输入参数：$*、$@⭐ <a class="header-anchor" href="#获取所有输入参数-、-⭐" aria-label="Permalink to &quot;获取所有输入参数：$*、$@⭐&quot;">​</a></h3><h4 id="语法规则-2" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-2" aria-label="Permalink to &quot;语法规则&quot;">​</a></h4><blockquote><p>#含义都是获取所有输入参数, 用于以后<strong>输出所有参数</strong></p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">$*</span></span>
<span class="line"><span style="color:#A6ACCD;font-style:italic;">$@</span></span></code></pre></div><p><code>$*</code>与<code>$@</code>区别</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 1.不使用双引号括起来, 功能一样</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># $*和$@获取所有输入参数,格式为: $1 $2 ... $n</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 2.使用双引号括起来</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &quot;$*&quot;获取的所有参数拼接为一个字符串, 格式为: &quot;$1 $2 ... $n&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &quot;$@&quot;获取一组参数列表对象, 格式为: &quot;$1&quot; &quot;$2&quot; ... &quot;$n&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用循环打印所有输入参数可以看出区别</span></span></code></pre></div><h4 id="循环语法" tabindex="-1">循环语法 <a class="header-anchor" href="#循环语法" aria-label="Permalink to &quot;循环语法&quot;">​</a></h4><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> var </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> 列表变量</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;">		 </span><span style="color:#676E95;font-style:italic;"># 循环开始</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#FFCB6B;">命令</span><span style="color:#A6ACCD;">   </span><span style="color:#676E95;font-style:italic;"># 循环体</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span><span style="color:#A6ACCD;">      </span><span style="color:#676E95;font-style:italic;"># 循环结束</span></span></code></pre></div><h4 id="案例演示-2" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-2" aria-label="Permalink to &quot;案例演示&quot;">​</a></h4><p>直接输出所有输入参数, 与循环方式输出所有输入参数(使用双引号包含 <code>$*</code> 与 <code>$@</code> )</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 命令1: 打印当前脚本文件名字</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">当前脚本文件名称:</span><span style="color:#A6ACCD;font-style:italic;">$0</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 命令2: 打印第1个输入参数</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第一个输入参数:</span><span style="color:#A6ACCD;font-style:italic;">$1</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 命令4: 打印第10个输入参数</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第十个输入参数带花括号获取:</span><span style="color:#A6ACCD;font-style:italic;">${10}</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 命令5 打印所有输入参数的个数</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">所有输入参数个数:</span><span style="color:#89DDFF;">${#}&quot;</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 增加命令: 实现直接输出所有输入后参数</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">使用$*直接输出:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">$*</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">使用$@直接输出:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;font-style:italic;">$@</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 增加命令: 使用$*循环打印输出所有输入参数</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">循环遍历输出$*所有参数</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> item </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$*</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $item</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 增加命令: 使用$@循环打印输出所有输入参数</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">循环遍历输出$@所有参数</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> item </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;font-style:italic;">$@</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $item</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><p>运行观察区别</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sh demo4</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">sh itcast heima renshuo</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241057828.png" alt="image-20220924105718671" style="zoom:80%;"><h3 id="获取函数返回值" tabindex="-1">获取函数返回值：$？ <a class="header-anchor" href="#获取函数返回值" aria-label="Permalink to &quot;获取函数返回值：$？&quot;">​</a></h3><h4 id="语法规则-3" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-3" aria-label="Permalink to &quot;语法规则&quot;">​</a></h4><blockquote><p><strong>用于获取上一个Shell命令的退出状态码, 或者是函数的返回值</strong></p><p><strong>每个Shell命令的执行都有一个返回值, 这个返回值用于说明命令执行是否成功</strong></p><p><strong>一般来说, 返回0代表命令执行成功, 非0代表执行失败</strong></p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">$?</span></span></code></pre></div><h4 id="案例演示-3" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-3" aria-label="Permalink to &quot;案例演示&quot;">​</a></h4><p>输入一个正确命令, 再输出<code>$?</code></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241100478.png" alt="image-20220924110010385" style="zoom:80%;"><p>输入一个错误命令, 在输出<code>$?</code></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241100927.png" alt="image-20220924110027825" style="zoom:80%;"><h3 id="获取当前shell环境进程id" tabindex="-1">获取当前Shell环境进程ID：$$ <a class="header-anchor" href="#获取当前shell环境进程id" aria-label="Permalink to &quot;获取当前Shell环境进程ID：$$&quot;">​</a></h3><p>查看当前Shell环境进程编号</p><div class="language-Shell"><button title="Copy Code" class="copy"></button><span class="lang">Shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ps</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-aux</span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">grep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $$</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241213927.png" alt="image-20220924121359785" style="zoom:80%;"><p>输出 $$ 显示当前shell环境进程编号</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241213181.png" alt="image-20220924121309082" style="zoom:80%;"><h2 id="环境变量加载流程" tabindex="-1">环境变量加载流程 <a class="header-anchor" href="#环境变量加载流程" aria-label="Permalink to &quot;环境变量加载流程&quot;">​</a></h2><p>注意：不同的工作环境加载环境变量流程不一样</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042118150.png" alt="image-20200608181518841"></p><h1 id="字符串变量" tabindex="-1">字符串变量 <a class="header-anchor" href="#字符串变量" aria-label="Permalink to &quot;字符串变量&quot;">​</a></h1><blockquote><p>字符串（String）就是一系列字符的组合。字符串是 Shell 编程中最常用的数据类型之一（除了数字和字符串，也没有其他类型了）</p></blockquote><h2 id="字符串格式" tabindex="-1">字符串格式 <a class="header-anchor" href="#字符串格式" aria-label="Permalink to &quot;字符串格式&quot;">​</a></h2><h3 id="定义方式" tabindex="-1">定义方式 <a class="header-anchor" href="#定义方式" aria-label="Permalink to &quot;定义方式&quot;">​</a></h3><blockquote><p>单引号方式、<strong>双引号方式(推荐)</strong>、不用引号方式</p></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">123</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">123</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">123</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241228029.png" alt="image-20220924122858930" style="zoom:80%;"><h3 id="格式区别" tabindex="-1">格式区别 <a class="header-anchor" href="#格式区别" aria-label="Permalink to &quot;格式区别&quot;">​</a></h3><blockquote><p>单引号, <strong>原样输出, 不会解析里面的变量</strong></p><p>双引号, <strong>会解析里面的变量, 并且可以使用子双引号, 需要转义</strong></p><p>不使用引号, <strong>也会解析里面的变量, 但是不能含有空格, 空格后面的字符串会作为命令去执行</strong></p></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1233</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">abc${age}</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">abc</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303111531437.png" alt="image-20230311153100327" style="zoom:80%;"><h2 id="字符串长度" tabindex="-1">字符串长度 <a class="header-anchor" href="#字符串长度" aria-label="Permalink to &quot;字符串长度&quot;">​</a></h2><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">${#</span><span style="color:#A6ACCD;">字符串变量名</span><span style="color:#89DDFF;">}</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">123</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${#</span><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241232550.png" alt="image-20220924123238451" style="zoom:80%;"><h2 id="字符串拼接" tabindex="-1">字符串拼接 <a class="header-anchor" href="#字符串拼接" aria-label="Permalink to &quot;字符串拼接&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119826.png" alt="image-20200610171003827" style="zoom:80%;"><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">你的年龄是</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">你的年龄是</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">age</span><span style="color:#89DDFF;">}</span></span></code></pre></div><h2 id="字符串截取⭐" tabindex="-1">字符串截取⭐ <a class="header-anchor" href="#字符串截取⭐" aria-label="Permalink to &quot;字符串截取⭐&quot;">​</a></h2><table><thead><tr><th>格式</th><th>说明</th></tr></thead><tbody><tr><td><code>${变量名:start:length}</code></td><td>从 string 字符串的左边第 start 个字符开始，<br>向右截取 length 个字符。start从0开始</td></tr><tr><td><code>${变量名:start}</code></td><td>从 string 字符串的左边第 start 个字符开始截取，直到最后。</td></tr><tr><td><code>${变量名:0-start:length}</code></td><td>从 string 字符串的右边第 start 个字符开始，<br>向右截取 length 个字符。start从1开始, 代表右侧第一个字符</td></tr><tr><td><code>${变量名:0-start}</code></td><td>从 string 字符串的右边第 start 个字符开始截取，直到最后。</td></tr><tr><td><code>${变量名#*chars}</code></td><td>从 string 字符串左边第一次出现 *chars 的位置开始，<br>截取 *chars 右边的所有字符。</td></tr><tr><td><code>${变量名##*chars}</code></td><td>从 string 字符串左边最后一次出现 *chars 的位置开始，<br>截取 *chars 右边的所有字符。</td></tr><tr><td><code>${变量名%chars*}</code></td><td>从 string 字符串右边第一次出现 chars* 的位置开始，<br>截取 chars* 左边的所有字符。</td></tr><tr><td><code>${变量名%%chars*}</code></td><td>从 string 字符串右边最后一次出现 chars* 的位置开始，<br>截取 chars* 左边的所有字符</td></tr></tbody></table><blockquote><p>字符串&quot;welcome to itheima&quot;</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">var1</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">welcome to itheima</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 从左侧第0个开始,向左截取2个字符：we</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">var1</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;">0</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;">2</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 从左侧第11个开始,向左截取所有字符：itheima</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">var1</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;">11</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 从右侧第5个开始,向右截取2个字符：he</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">var1</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;">0-5</span><span style="color:#89DDFF;">:</span><span style="color:#A6ACCD;">2</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 截取左边第一次出现字符e右边的所有字符：lcome to itheima</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">var1</span><span style="color:#89DDFF;">#*</span><span style="color:#A6ACCD;">e</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 截取左边最后一次出现字符e右边的所有字符：ima</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">var1</span><span style="color:#89DDFF;">##*</span><span style="color:#A6ACCD;">e</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 截取右边第一次次出现字符e左边的所有字符：welcome to ith</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">var1</span><span style="color:#89DDFF;">%</span><span style="color:#A6ACCD;">e</span><span style="color:#89DDFF;">*}</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 截取右边最后一次出现字符e左边的所有字符：w</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">var1</span><span style="color:#89DDFF;">%%</span><span style="color:#A6ACCD;">e</span><span style="color:#89DDFF;">*}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241244437.png" alt="image-20220924124401289" style="zoom:80%;"><h1 id="索引数组变量" tabindex="-1">索引数组变量 <a class="header-anchor" href="#索引数组变量" aria-label="Permalink to &quot;索引数组变量&quot;">​</a></h1><blockquote><p>注意Bash Shell 只支持一维数组，不支持多维数组。</p></blockquote><h2 id="数组定义" tabindex="-1">数组定义 <a class="header-anchor" href="#数组定义" aria-label="Permalink to &quot;数组定义&quot;">​</a></h2><blockquote><p>在 Shell 中，用括号<code>( )</code>来表示数组，数组元素之间用空格来分隔，注意，赋值号 <code>=</code> 两边不能有空格</p></blockquote><p>1.定义数组，两种方式，内容随便，shell是弱类型</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 1、定义数字存储100,3,22,58,77,17,20</span></span>
<span class="line"><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">=(</span><span style="color:#F78C6C;">29</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">13</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">8</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">91</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 2、指定元素赋值初始化，由于只赋值了3个元素, 所以数组的长度是3</span></span>
<span class="line"><span style="color:#A6ACCD;">arr1</span><span style="color:#89DDFF;">=(</span><span style="color:#C3E88D;">[</span><span style="color:#F78C6C;">0</span><span style="color:#C3E88D;">]=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">[</span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;">]=</span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">[</span><span style="color:#F78C6C;">4</span><span style="color:#C3E88D;">]=renshuo</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 3、Shell 是弱类型的，它并不要求所有数组元素的类型必须相同</span></span>
<span class="line"><span style="color:#A6ACCD;">arr3</span><span style="color:#89DDFF;">=(</span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">56</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">http://www.itcast.cn/</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>2.Shell数组元素定义后不是固定的, 定义后还可以赋值</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">6</span><span style="color:#89DDFF;">]=</span><span style="color:#F78C6C;">100</span></span></code></pre></div><h2 id="数组元素获取" tabindex="-1">数组元素获取 <a class="header-anchor" href="#数组元素获取" aria-label="Permalink to &quot;数组元素获取&quot;">​</a></h2><p>1.通过下标获取元素值,index从0开始</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">index</span><span style="color:#89DDFF;">]}</span></span></code></pre></div><blockquote><p>注意使用<code>{ }</code></p></blockquote><p>2.获取值同时复制给其他变量</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">item</span><span style="color:#89DDFF;">=${</span><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">index</span><span style="color:#89DDFF;">]}</span></span></code></pre></div><p>3.使用 <code>@</code> 或 <code>*</code> 可以获取数组中的所有元素</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">]}</span></span>
<span class="line"><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">*</span><span style="color:#89DDFF;">]}</span></span></code></pre></div><p>4.获取数组的长度或个数</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">${#</span><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">]}</span></span>
<span class="line"><span style="color:#89DDFF;">${#</span><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">*</span><span style="color:#89DDFF;">]}</span></span></code></pre></div><p>5.获取数组指定元素的字符长度</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">${#</span><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">索引</span><span style="color:#89DDFF;">]}</span></span></code></pre></div><h2 id="数组拼接" tabindex="-1">数组拼接 <a class="header-anchor" href="#数组拼接" aria-label="Permalink to &quot;数组拼接&quot;">​</a></h2><p>所谓 Shell 数组拼接（数组合并），就是将两个数组连接成一个数组</p><p>使用 <code>@</code> 和 <code>*</code> 获取数组所有元素之后进行拼接</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">array_new</span><span style="color:#89DDFF;">=(${</span><span style="color:#A6ACCD;">array1</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">]}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">array2</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">]}</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">...</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">array_new</span><span style="color:#89DDFF;">=(${</span><span style="color:#A6ACCD;">array1</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">*</span><span style="color:#89DDFF;">]}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">array2</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">*</span><span style="color:#89DDFF;">]}</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">...</span><span style="color:#89DDFF;">)</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">nums</span><span style="color:#89DDFF;">=(</span><span style="color:#F78C6C;">29</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">13</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">8</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">91</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">44</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">nums2</span><span style="color:#89DDFF;">=(</span><span style="color:#F78C6C;">29</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">13</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">8</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">91</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">44</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">nums3</span><span style="color:#89DDFF;">=(${</span><span style="color:#A6ACCD;">nums</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">]}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">nums2</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">]})</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">nums3</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">]}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241257955.png" alt="image-20220924125740840" style="zoom:80%;"><h2 id="数组删除" tabindex="-1">数组删除 <a class="header-anchor" href="#数组删除" aria-label="Permalink to &quot;数组删除&quot;">​</a></h2><p>删除数组指定元素数据和删除整个数组数据</p><p><strong>删除数组指定元素数据</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">unset</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">array_name[index]</span></span>
<span class="line"><span style="color:#82AAFF;">unset</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">arr[</span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">]</span></span></code></pre></div><p><strong>删除整个数组</strong></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">unset</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">array_name</span></span></code></pre></div><h1 id="内置命令⭐" tabindex="-1">内置命令⭐ <a class="header-anchor" href="#内置命令⭐" aria-label="Permalink to &quot;内置命令⭐&quot;">​</a></h1><h2 id="内置命令介绍" tabindex="-1">内置命令介绍 <a class="header-anchor" href="#内置命令介绍" aria-label="Permalink to &quot;内置命令介绍&quot;">​</a></h2><p>Shell 内置命令，就是由 Bash Shell 自身提供的命令，而不是文件系统中的可执行脚本文件。</p><p>使用type 来确定一个命令是否是内置命令：</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">type 命令</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119135.png" alt="image-20200630083936549" style="zoom:80%;"><blockquote><p>通常来说，内置命令会比外部命令执行得更快，执行外部命令时不但会触发磁盘 I/O，还需要 fork 出一个单独的进程来执行，执行完成后再退出。而执行内置命令相当于调用当前 Shell 进程的一个函数, 还是在当前Shell环境进程内, 减少了上下文切换。</p></blockquote><h2 id="内置命令列表" tabindex="-1">内置命令列表 <a class="header-anchor" href="#内置命令列表" aria-label="Permalink to &quot;内置命令列表&quot;">​</a></h2><table><thead><tr><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>:</td><td>扩展参数列表，执行重定向操作</td></tr><tr><td>.</td><td>读取并执行指定文件中的命令（在当前 shell 环境中）</td></tr><tr><td>==alias==</td><td>为指定命令定义一个别名</td></tr><tr><td>bg</td><td>将作业以后台模式运行</td></tr><tr><td>bind</td><td>将键盘序列绑定到一个 readline 函数或宏</td></tr><tr><td>break</td><td>退出 for、while、select 或 until 循环</td></tr><tr><td>builtin</td><td>执行指定的 shell 内建命令</td></tr><tr><td>caller</td><td>返回活动子函数调用的上下文</td></tr><tr><td>cd</td><td>将当前目录切换为指定的目录</td></tr><tr><td>command</td><td>执行指定的命令，无需进行通常的 shell 查找</td></tr><tr><td>compgen</td><td>为指定单词生成可能的补全匹配</td></tr><tr><td>complete</td><td>显示指定的单词是如何补全的</td></tr><tr><td>compopt</td><td>修改指定单词的补全选项</td></tr><tr><td>continue</td><td>继续执行 for、while、select 或 until 循环的下一次迭代</td></tr><tr><td>==declare==</td><td>声明一个变量或变量类型。</td></tr><tr><td>dirs</td><td>显示当前存储目录的列表</td></tr><tr><td>disown</td><td>从进程作业表中刪除指定的作业</td></tr><tr><td>==echo==</td><td>将指定字符串输出到 STDOUT</td></tr><tr><td>enable</td><td>启用或禁用指定的内建shell命令</td></tr><tr><td>eval</td><td>将指定的参数拼接成一个命令，然后执行该命令</td></tr><tr><td>exec</td><td>用指定命令替换 shell 进程</td></tr><tr><td>==exit==</td><td>强制 shell 以指定的退出状态码退出</td></tr><tr><td>export</td><td>设置子 shell 进程可用的变量</td></tr><tr><td>fc</td><td>从历史记录中选择命令列表</td></tr><tr><td>fg</td><td>将作业以前台模式运行</td></tr><tr><td>getopts</td><td>分析指定的位置参数</td></tr><tr><td>hash</td><td>查找并记住指定命令的全路径名</td></tr><tr><td>help</td><td>显示帮助文件</td></tr><tr><td>history</td><td>显示命令历史记录</td></tr><tr><td>jobs</td><td>列出活动作业</td></tr><tr><td>kill</td><td>向指定的进程 ID(PID) 发送一个系统信号</td></tr><tr><td>let</td><td>计算一个数学表达式中的每个参数</td></tr><tr><td>local</td><td>在函数中创建一个作用域受限的变量</td></tr><tr><td>logout</td><td>退出登录 shell</td></tr><tr><td>mapfile</td><td>从 STDIN 读取数据行，并将其加入索引数组</td></tr><tr><td>popd</td><td>从目录栈中删除记录</td></tr><tr><td>printf</td><td>使用格式化字符串显示文本</td></tr><tr><td>pushd</td><td>向目录栈添加一个目录</td></tr><tr><td>pwd</td><td>显示当前工作目录的路径名</td></tr><tr><td>==read==</td><td>从 STDIN 读取一行数据并将其赋给一个变量</td></tr><tr><td>readarray</td><td>从 STDIN 读取数据行并将其放入索引数组</td></tr><tr><td>readonly</td><td>从 STDIN 读取一行数据并将其赋给一个不可修改的变量</td></tr><tr><td>return</td><td>强制函数以某个值退出，这个值可以被调用脚本提取</td></tr><tr><td>set</td><td>设置并显示环境变量的值和 shell 属性</td></tr><tr><td>shift</td><td>将位置参数依次向下降一个位置</td></tr><tr><td>shopt</td><td>打开/关闭控制 shell 可选行为的变量值</td></tr><tr><td>source</td><td>读取并执行指定文件中的命令（在当前 shell 环境中）</td></tr><tr><td>suspend</td><td>暂停 Shell 的执行，直到收到一个 SIGCONT 信号</td></tr><tr><td>test</td><td>基于指定条件返回退出状态码 0 或 1</td></tr><tr><td>times</td><td>显示累计的用户和系统时间</td></tr><tr><td>trap</td><td>如果收到了指定的系统信号，执行指定的命令</td></tr><tr><td>type</td><td>显示指定的单词如果作为命令将会如何被解释</td></tr><tr><td>typeset</td><td>声明一个变量或变量类型。</td></tr><tr><td>ulimit</td><td>为系统用户设置指定的资源的上限</td></tr><tr><td>umask</td><td>为新建的文件和目录设置默认权限</td></tr><tr><td>unalias</td><td>刪除指定的别名</td></tr><tr><td>unset</td><td>刪除指定的环境变量或 shell 属性</td></tr><tr><td>wait</td><td>等待指定的进程完成，并返回退出状态码</td></tr></tbody></table><h2 id="alias设置别名⭐" tabindex="-1">alias设置别名⭐ <a class="header-anchor" href="#alias设置别名⭐" aria-label="Permalink to &quot;alias设置别名⭐&quot;">​</a></h2><blockquote><p>alisa 用于给命令创建别名。<strong>可以将经常操作比较复杂的命令进行设置别名, 通过别名的操作提高工作效率</strong></p></blockquote><h3 id="定义-删除别名" tabindex="-1">定义 | 删除别名 <a class="header-anchor" href="#定义-删除别名" aria-label="Permalink to &quot;定义 | 删除别名&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 设置别名</span></span>
<span class="line"><span style="color:#82AAFF;">alias</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">别名=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">命令</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 删除指定的别名</span></span>
<span class="line"><span style="color:#82AAFF;">unalias</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">别名</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 删除当前Shell环境中所有别名</span></span>
<span class="line"><span style="color:#82AAFF;">unalias</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-a</span></span></code></pre></div><blockquote><p>注意: 以上2种方式删除都是临时删除当前Shell的别名, 如果想永久删除必须去配置文件中手动删除</p></blockquote><h3 id="查看全部别名" tabindex="-1">查看全部别名 <a class="header-anchor" href="#查看全部别名" aria-label="Permalink to &quot;查看全部别名&quot;">​</a></h3><p>上面是系统为了方便命令操作默认将部分命令创建为别名 <code>ll</code> 的命令与<code>ls -l</code>的效果一样, 就是因为ll是别名</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303111553591.png" alt="image-20230311155316470" style="zoom:80%;"><h3 id="持久化别名⭐" tabindex="-1">持久化别名⭐ <a class="header-anchor" href="#持久化别名⭐" aria-label="Permalink to &quot;持久化别名⭐&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vi</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/profile</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 直接在最后写入如下内容</span></span>
<span class="line"><span style="color:#C792EA;">alias</span><span style="color:#A6ACCD;"> psa</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">ps -aux</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 刷新配置文件</span></span>
<span class="line"><span style="color:#82AAFF;">source</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/profile</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用命令能够实现上面ps -aux效果</span></span>
<span class="line"><span style="color:#FFCB6B;">psa</span></span></code></pre></div><h3 id="实战演练⭐-1" tabindex="-1">实战演练⭐ <a class="header-anchor" href="#实战演练⭐-1" aria-label="Permalink to &quot;实战演练⭐&quot;">​</a></h3><p>输出所有进程信息</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 基础命令</span></span>
<span class="line"><span style="color:#FFCB6B;">ps</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-aux</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 给上面的命令起别名为psList, 并使用psList输出</span></span>
<span class="line"><span style="color:#C792EA;">alias</span><span style="color:#A6ACCD;"> psList</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">ps -aux</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用别名，作用就是基础命令</span></span>
<span class="line"><span style="color:#FFCB6B;">psList</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241528826.png" alt="image-20220924152851666" style="zoom:80%;"><p>删除psList别名</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241530929.png" alt="image-20220924153046823" style="zoom:80%;"><h2 id="echo输出字符串" tabindex="-1">echo输出字符串 <a class="header-anchor" href="#echo输出字符串" aria-label="Permalink to &quot;echo输出字符串&quot;">​</a></h2><blockquote><p>echo 是一个 Shell 内置命令，用于在终端输出字符串，并在最后默认加上换行符</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 默认输出换行语法</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Hello World</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Hello World，</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">name</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -n：不在输出末尾添加换行符</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">字符串</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -e：输出带有转义字符的文本</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-e</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">Hello\tWorld\nGoodbye</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># \c：用于强制清除echo的结尾换行输出</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-e</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello world\c</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><h2 id="read读取控制台输入" tabindex="-1">read读取控制台输入 <a class="header-anchor" href="#read读取控制台输入" aria-label="Permalink to &quot;read读取控制台输入&quot;">​</a></h2><blockquote><p>read 是 Shell 内置命令，用于从标准输入中读取数据并赋值给变量。如果没有进行重定向，默认就是从终端控制台读取用户输入的数据；如果进行了重定向，那么可以从文件中读取数据。</p></blockquote><h3 id="基本语法" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">read </span><span style="color:#89DDFF;">[-</span><span style="color:#A6ACCD;">options</span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">var1 var2 </span><span style="color:#89DDFF;">...]</span></span></code></pre></div><blockquote><p><code>options</code>表示选项，如下表所示；<code>var</code>表示用来存储数据的变量，可以有一个，也可以有多个。</p><p><code>options</code>和<code>var</code>都是可选的，如果没有提供变量名，那么读取的数据将存放到环境变量 <code>REPLY</code> 变量中。</p><p>$REPLY保存read最后一个读入命令的数据</p></blockquote><p>options支持的参数</p><table><thead><tr><th>选项</th><th>说明</th></tr></thead><tbody><tr><td>-a array</td><td>把读取的数据赋值给数组 array，从下标 0 开始。</td></tr><tr><td>-d delimiter</td><td>用字符串 delimiter 指定读取结束的位置，而不是一个换行符（读取到的数据不包括 delimiter）。</td></tr><tr><td>-e</td><td>在获取用户输入的时候，对功能键进行编码转换，不会直接显式功能键对应的字符。</td></tr><tr><td>-n num</td><td>读取 num 个字符，而不是整行字符。</td></tr><tr><td>-p prompt</td><td>显示提示信息，提示内容为 prompt。</td></tr><tr><td>-r</td><td>原样读取（Raw mode），不把反斜杠字符解释为转义字符。</td></tr><tr><td>-s</td><td>静默模式（Silent mode），不会在屏幕上显示输入的字符。<br>当输入密码和其它确认信息的时候，这是很有必要的。</td></tr><tr><td>-t seconds</td><td>设置超时时间，单位为秒。如果用户没有在指定时间内输入完成，<br>那么 read 将会返回一个非 0 的退出状态，表示读取失败。</td></tr><tr><td>-u fd</td><td>使用文件描述符 fd 作为输入源，而不是标准输入，类似于重定向。</td></tr></tbody></table><h3 id="示例1-多个变量赋值" tabindex="-1">示例1：多个变量赋值 <a class="header-anchor" href="#示例1-多个变量赋值" aria-label="Permalink to &quot;示例1：多个变量赋值&quot;">​</a></h3><blockquote><p>使用read命令读取数据,要有提示信息&quot;请输入姓名,年龄,爱好:&quot; 将数据赋值给多个变量，打印每一个变量的值</p></blockquote><p>编辑文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">read1.sh</span></span></code></pre></div><p>read1.sh文件内容</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入姓名,年龄,爱好: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">name</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">age</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">hobby</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">姓名：</span><span style="color:#A6ACCD;">$name</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">年龄：</span><span style="color:#A6ACCD;">$age</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">爱好：</span><span style="color:#A6ACCD;">$hobby</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241509302.png" alt="image-20220924150919156" style="zoom:80%;"><h3 id="示例2-读取一个字符" tabindex="-1">示例2：读取一个字符 <a class="header-anchor" href="#示例2-读取一个字符" aria-label="Permalink to &quot;示例2：读取一个字符&quot;">​</a></h3><blockquote><p>使用read命令读取数据,要有提示信息&quot;您确定要删除数据吗?(请输入y/n):&quot; 并且设置读取一个字符打印这个字符输出</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">read2.sh</span></span></code></pre></div><p>read2.sh文件内容</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># char不是数据类型，而是接收的一个字符</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">请输入一个字符: </span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">var1</span></span>
<span class="line"><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">\n</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">你输入的字符为:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">$var1</span></span></code></pre></div><blockquote><ul><li><code>-n 1</code>：表示只读取一个字符。</li><li><code>-p &#39;请输入一个字符: &#39;</code>：表示在读取用户输入之前，输出提示信息&quot;请输入一个字符：&quot;。</li><li><code>var1</code>：表示将读取的字符赋值给变量<code>var1</code>。</li></ul></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241515514.png" alt="image-20220924151546403" style="zoom:80%;"><h3 id="示例3-限制时间输入" tabindex="-1">示例3：限制时间输入 <a class="header-anchor" href="#示例3-限制时间输入" aria-label="Permalink to &quot;示例3：限制时间输入&quot;">​</a></h3><blockquote><p>在终端控制台输入时, 设置指定时间内输入密码，read限制时间输入：<code>read -t 秒数 var1</code></p></blockquote><p>编辑文件read3.sh脚本文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 使用read命令读取数据,要有提示信息&quot;请输入密码(20秒内):&quot; 并且设置限制时间20秒</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 输出一个换行</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用read命令读取数据,要有提示信息&quot;请再次输入密码(20秒内):&quot; 并且设置限制时间20秒</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 输出一个换行</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 校验密码与再次输入的密码是否一致</span></span></code></pre></div><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">read3.sh</span></span></code></pre></div><p>read3.sh文件内容</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-sp</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">请输入密码(20秒内):</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">pwd1</span></span>
<span class="line"><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">\n</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-sp</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">请再次输入密码(20秒内):</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">pwd2</span></span>
<span class="line"><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">\n</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $pwd1 </span><span style="color:#89DDFF;">==</span><span style="color:#A6ACCD;"> $pwd2 </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">密码与确认密码一致, 验证通过</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">密码与确认密码不一致,验证失败</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>执行文件运行效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241518559.png" alt="image-20220924151850452" style="zoom:80%;"><h3 id="示例4-读取并赋值给数组" tabindex="-1">示例4：读取并赋值给数组 <a class="header-anchor" href="#示例4-读取并赋值给数组" aria-label="Permalink to &quot;示例4：读取并赋值给数组&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 定义一个空数组</span></span>
<span class="line"><span style="color:#A6ACCD;">my_array</span><span style="color:#89DDFF;">=()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 循环读取用户输入</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">; i</span><span style="color:#89DDFF;">&lt;</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">; i</span><span style="color:#89DDFF;">++));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入第 </span><span style="color:#89DDFF;">$((</span><span style="color:#C3E88D;">i</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">))</span><span style="color:#C3E88D;"> 个值：</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">input_value</span></span>
<span class="line"><span style="color:#A6ACCD;">  my_array</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">$i</span><span style="color:#89DDFF;">]=</span><span style="color:#A6ACCD;">$input_value</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 输出数组的值</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">数组的值为：</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;${</span><span style="color:#A6ACCD;">my_array</span><span style="color:#89DDFF;">[</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">]}&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303111631824.png" alt="image-20230311163131669" style="zoom:80%;"><h2 id="exit退出" tabindex="-1">exit退出 <a class="header-anchor" href="#exit退出" aria-label="Permalink to &quot;exit退出&quot;">​</a></h2><blockquote><p>exit用于退出当前Shell环境进程结束运行, 并且可以返回一个状态码.一般使用 <code>$?</code> 可以获取退出状态码。比如执行一个脚本文件里面操作一个文件时，可以返回1 表示文件不存在，2 表示文件没有读取权限，3 表示文件类型不对</p></blockquote><h3 id="基本语法-1" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-1" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><p>正确退出语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">exit</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># 默认返回状态码0, 一般代表命令执行成功</span></span></code></pre></div><p>错误退出语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">exit</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">非0数字</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;"># 数字建议的范围0~255,  一般代表命令执行失败</span></span></code></pre></div><h3 id="示例-使用exit退出" tabindex="-1">示例：使用exit退出 <a class="header-anchor" href="#示例-使用exit退出" aria-label="Permalink to &quot;示例：使用exit退出&quot;">​</a></h3><blockquote><p>编写Shell脚本使用exit 退出, 退出时返回一个非0数字状态值, 执行脚本文件并打印返回状态值</p></blockquote><p>编辑文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">exit.sh</span></span></code></pre></div><p>exit.sh文件内容: 使用<code>exit 数字</code> 退出结束当前Shell</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">one</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#82AAFF;">exit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">6</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">two</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119488.png" alt="image-20200630150541964"></p><h2 id="declare设置变量属性" tabindex="-1">declare设置变量属性 <a class="header-anchor" href="#declare设置变量属性" aria-label="Permalink to &quot;declare设置变量属性&quot;">​</a></h2><blockquote><p>declare命令用于声明 shell 变量。可用来声明变量并设置变量的属性，也可用来显示shell函数。若不加上任何参数，则会显示全部的shell变量与函数(与执行set指令的效果相同)。</p></blockquote><h3 id="declare命令作用" tabindex="-1">declare命令作用 <a class="header-anchor" href="#declare命令作用" aria-label="Permalink to &quot;declare命令作用&quot;">​</a></h3><blockquote><p>1.declare设置变量的属性[重要]</p><p>2.查看全部Shell变量与函数</p><p>3.实现关联数组变量[重要]</p></blockquote><h3 id="declare语法" tabindex="-1">declare语法 <a class="header-anchor" href="#declare语法" aria-label="Permalink to &quot;declare语法&quot;">​</a></h3><h4 id="设置语法" tabindex="-1">设置语法 <a class="header-anchor" href="#设置语法" aria-label="Permalink to &quot;设置语法&quot;">​</a></h4><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">declare </span><span style="color:#89DDFF;">[+/-][</span><span style="color:#A6ACCD;">aArxif</span><span style="color:#89DDFF;">][</span><span style="color:#A6ACCD;">变量名称＝设置值</span><span style="color:#89DDFF;">]</span></span></code></pre></div><blockquote><p>+/- 　&quot;-&quot;可用来指定变量的属性，&quot;+&quot;则是取消变量所设的属性。</p><p>a array,设置为普通索引数组</p><p>A Array,设置为key-value关联数组</p><p>r 　readonly,将变量设置为只读, 也可以使用readonly</p><p>x 　exprot,设置变量成为全局变量，也可以使用export</p><p>i 　int,设置为整型变量。</p><p>f function,设置为一个函数变量</p></blockquote><h4 id="查看全部shell变量与函数语法" tabindex="-1">查看全部Shell变量与函数语法 <a class="header-anchor" href="#查看全部shell变量与函数语法" aria-label="Permalink to &quot;查看全部Shell变量与函数语法&quot;">​</a></h4><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">declare </span><span style="color:#89DDFF;">[-</span><span style="color:#A6ACCD;">fF</span><span style="color:#89DDFF;">]</span></span></code></pre></div><blockquote><p>declare 后无参数, 查询全部Shell变量与函数定义, 与set功能一模一样</p><p>-f 　查询仅显示函数定义。</p><p>-F 查询仅显示函数名字</p></blockquote><h4 id="key-value关联数组变量语法" tabindex="-1">key-value关联数组变量语法 <a class="header-anchor" href="#key-value关联数组变量语法" aria-label="Permalink to &quot;key-value关联数组变量语法&quot;">​</a></h4><p>关联数组也称为“键值对（key-value）”数组，键（key）也即字符串形式的数组下标，值（value）也即元素值。</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-A</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">关联数组变量名=</span><span style="color:#A6ACCD;">(</span><span style="color:#C3E88D;">[字符串key1]=值1</span><span style="color:#A6ACCD;"> [字符串key2]=值2 ...)</span></span></code></pre></div><blockquote><p>declare也可以用于定义普通索引数组, <code>-a</code> 参数创建普通或索引数组 <code>-A</code> 创建关联数组</p><p><code>declare -a 关联数组变量名=(值1 值2 ...)</code></p><p><code>declare -a 关联数组变量名=([0]=值1 [1]=值2 ...)</code></p></blockquote><p>获取指定key的值</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">关联数组变量名</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">key</span><span style="color:#89DDFF;">]}</span></span></code></pre></div><p>获取所有值</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">关联数组变量名</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">*</span><span style="color:#89DDFF;">]}</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;"># 方式1</span></span>
<span class="line"><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">关联数组变量名</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">@</span><span style="color:#89DDFF;">]}</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;"># 方式2</span></span></code></pre></div><h3 id="示例1-设置变量属性" tabindex="-1">示例1：设置变量属性 <a class="header-anchor" href="#示例1-设置变量属性" aria-label="Permalink to &quot;示例1：设置变量属性&quot;">​</a></h3><blockquote><p>操作一个变量属性, 设置为整型\ 取消整型\设置为只读等操作</p></blockquote><blockquote><ol><li>使用declare设置一个整型变量并赋值</li><li>取消变量的整型类型, 尝试设置其他类型值尝试</li><li>设置变量只读, 尝试赋值测试</li></ol></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119492.png" alt="image-20200630164537518" style="zoom:80%;"><h3 id="示例2-查看当前shell所有函数名" tabindex="-1">示例2：查看当前Shell所有函数名 <a class="header-anchor" href="#示例2-查看当前shell所有函数名" aria-label="Permalink to &quot;示例2：查看当前Shell所有函数名&quot;">​</a></h3><p>查看所有变量与所有函数</p><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">declare</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119500.png" alt="image-20200701093735283" style="zoom:80%;"><p>查看所有函数与定义</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119503.png" alt="image-20200701094119327" style="zoom:80%;"><p>查询所有函数名列表</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119210.png" alt="image-20200701094553443"></p><h3 id="示例3-实现普通索引数组" tabindex="-1">示例3：实现普通索引数组 <a class="header-anchor" href="#示例3-实现普通索引数组" aria-label="Permalink to &quot;示例3：实现普通索引数组&quot;">​</a></h3><blockquote><p>使用declare定义一普通数组,并获取打印元素数据的值</p></blockquote><p>1.创建declare1.sh文件</p><p>2.编辑declare1.sh文件内容, 使用declare定义普通数组数据与打印</p><p>2.执行文件</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">vim</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">declare1.sh</span></span></code></pre></div><p>declare1.sh文件内容</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">arr1=</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">aa</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">arr1</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">1</span><span style="color:#89DDFF;">]}</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">arr1</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">*</span><span style="color:#89DDFF;">]}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">arr2=</span><span style="color:#A6ACCD;">(</span><span style="color:#C3E88D;">[</span><span style="color:#F78C6C;">0</span><span style="color:#C3E88D;">]=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> [1]=2  </span><span style="color:#89DDFF;">[</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;">=3  </span><span style="color:#89DDFF;">[</span><span style="color:#F78C6C;">4</span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;">=aa)</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">arr2</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">1</span><span style="color:#89DDFF;">]}</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">arr2</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">*</span><span style="color:#89DDFF;">]}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">arr2[</span><span style="color:#F78C6C;">3</span><span style="color:#C3E88D;">]=</span><span style="color:#F78C6C;">4</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">arr2</span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">*</span><span style="color:#89DDFF;">]}</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119215.png" alt="image-20200701101651788" style="zoom:80%;"><h3 id="示例4-实现key-value关联数组" tabindex="-1">示例4：实现key-value关联数组 <a class="header-anchor" href="#示例4-实现key-value关联数组" aria-label="Permalink to &quot;示例4：实现key-value关联数组&quot;">​</a></h3><blockquote><p>使用declare定义关联数组并打印输出</p></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 创建索引数组</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">创建索引数组</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">array1=</span><span style="color:#A6ACCD;">(</span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">abc</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 获取索引数组数据</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">获取array1数组第3个元素: </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">array1</span><span style="color:#89DDFF;">[</span><span style="color:#C3E88D;">2</span><span style="color:#89DDFF;">]}&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">打印array1数组所有元素:</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">array1</span><span style="color:#89DDFF;">[</span><span style="color:#C3E88D;">*</span><span style="color:#89DDFF;">]}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 创建关联数组</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">创建关联数组</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-A</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">array2=</span><span style="color:#A6ACCD;">(</span><span style="color:#C3E88D;">[</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">one</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">]=</span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> [</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">two</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">]=abc </span><span style="color:#89DDFF;">[</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itcast</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 获取索引数组数据</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">获取array2数组key=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itcast</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">元素: </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">array2</span><span style="color:#89DDFF;">[</span><span style="color:#C3E88D;">&quot;itcast&quot;</span><span style="color:#89DDFF;">]}&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">打印array2数组所有元素:</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">array2</span><span style="color:#89DDFF;">[</span><span style="color:#C3E88D;">@</span><span style="color:#89DDFF;">]}&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241600666.png" alt="image-20220924160044527" style="zoom:80%;"><blockquote><p>注意: 使用参数<code>-A</code> 才是关联数组 , 关联数组无法使用索引获取</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119217.png" alt="image-20200701105943576"></p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119224.png" alt="image-20200708093710733"></p><h1 id="运算符" tabindex="-1">运算符 <a class="header-anchor" href="#运算符" aria-label="Permalink to &quot;运算符&quot;">​</a></h1><h2 id="算术运算符" tabindex="-1">算术运算符 <a class="header-anchor" href="#算术运算符" aria-label="Permalink to &quot;算术运算符&quot;">​</a></h2><blockquote><p>原生bash不支持简单的数学运算，但是可以通过其他命令来实现，例如 awk 和 expr，expr 最常用。expr 是一款表达式计算工具，使用它能完成表达式的求值操作。</p></blockquote><h3 id="基本语法-2" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-2" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 例如，两个数相加</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 注意使用的是反引号 ` 而不是单引号 &#39;，只能进行整数运算，不能小数运算</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># `expr 算术运算符表达式`,运算符表达式中每个数字与符号之间要有空格</span></span>
<span class="line"><span style="color:#FFCB6B;">val</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;"> + </span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">两数之和为 : </span><span style="color:#A6ACCD;">$val</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">两数之和为 : 4</span></span></code></pre></div><blockquote><ul><li>表达式和运算符之间要有空格，例如 2+2 是不对的，必须写成 2 + 2</li><li>完整的表达式要被反引号包含，注意这个字符不是常用的单引号，在 Esc 键下边。</li></ul></blockquote><h3 id="实战演示" tabindex="-1">实战演示 <a class="header-anchor" href="#实战演示" aria-label="Permalink to &quot;实战演示&quot;">​</a></h3><blockquote><ul><li>乘号前边必须加反斜杠才能实现乘法运算；</li><li>if...then...fi 是条件语句，后续将会讲解。</li><li>在 MAC 中 shell 的 expr 语法是：$((表达式))，此处表达式中的 &quot;*&quot; 不需要转义符号 &quot;&quot; 。</li></ul></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 使用read命令读取输入2个数字，-p：输入一个字符</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入第一个数字:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">a</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入第二个数字:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">b</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 对2个数字进行算术运算</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a+b=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> + </span><span style="color:#A6ACCD;">$b</span><span style="color:#89DDFF;">`&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a-b=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> - </span><span style="color:#A6ACCD;">$b</span><span style="color:#89DDFF;">`&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a*b=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">\*</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">$b</span><span style="color:#89DDFF;">`&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">b/a=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> / </span><span style="color:#A6ACCD;">$a</span><span style="color:#89DDFF;">`&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">b%a=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> % </span><span style="color:#A6ACCD;">$a</span><span style="color:#89DDFF;">`&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241607089.png" alt="image-20220924160719953" style="zoom:80%;"><h2 id="整数比较运算符" tabindex="-1">整数比较运算符 <a class="header-anchor" href="#整数比较运算符" aria-label="Permalink to &quot;整数比较运算符&quot;">​</a></h2><h3 id="语法规则-4" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-4" aria-label="Permalink to &quot;语法规则&quot;">​</a></h3><p>下表列出了常用的比较运算符，假定变量 a 为 1，变量 b 为 2：</p><table><thead><tr><th style="text-align:left;">运算符</th><th style="text-align:left;">说明</th><th style="text-align:left;">举例</th></tr></thead><tbody><tr><td style="text-align:left;"><code>-eq</code></td><td style="text-align:left;">equals 检测两个数是否相等，相等返回 0, 否则返回1。</td><td style="text-align:left;"><code>[ $a -eq $b ]</code> 返回 1。</td></tr><tr><td style="text-align:left;"><code>-ne</code></td><td style="text-align:left;">not equals检测两个数是否不相等，不相等返回 true。</td><td style="text-align:left;"><code>[ $a -ne $b ]</code> 返回 0。</td></tr><tr><td style="text-align:left;"><code>-gt</code></td><td style="text-align:left;">greater than检测左边的数是否大于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>[ $a -gt $b ]</code> 返回 1。</td></tr><tr><td style="text-align:left;"><code>-lt</code></td><td style="text-align:left;">lower than检测左边的数是否小于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>[ $a -lt $b ]</code> 返回 0。</td></tr><tr><td style="text-align:left;"><code>-ge</code></td><td style="text-align:left;">greater equals检测左边的数是否大于等于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>[ $a -ge $b ] </code>返回 1。</td></tr><tr><td style="text-align:left;"><code>-le</code></td><td style="text-align:left;">lower equals检测左边的数是否小于等于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>[ $a -le $b ] </code>返回 0。</td></tr><tr><td style="text-align:left;"><code>&lt;</code></td><td style="text-align:left;">检测左边的数是否小于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>(($a&lt;$b))</code> 返回0</td></tr><tr><td style="text-align:left;"><code>&lt;=</code></td><td style="text-align:left;">检测左边的数是否小于等于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>(($a&lt;=$b))</code> 返回0</td></tr><tr><td style="text-align:left;"><code>&gt;</code></td><td style="text-align:left;">检测左边的数是否大于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>(($a&gt;$b))</code> 返回1</td></tr><tr><td style="text-align:left;"><code>&gt;=</code></td><td style="text-align:left;">检测左边的数是否大于等于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>(($a&gt;=$b))</code> 返回1</td></tr><tr><td style="text-align:left;"><code>==</code></td><td style="text-align:left;">检测左边的数是否等于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>(($a==$b))</code> 返回1</td></tr><tr><td style="text-align:left;"><code>!=</code></td><td style="text-align:left;">检测左边的数是否不等于右边的,<br>是返回0, 否则1</td><td style="text-align:left;"><code>(($a!=$b))</code> 返回0</td></tr></tbody></table><blockquote><p>注意:</p><p>整数比较运算符只支持整数，不支持小数与字符串(字符串比较后续讲解)，除非字符串的值是整数数字。</p><p>每个命令都有返回值, 这个后面我们会讲解退出状态再具体说明, 返回0代表成功, 返回1代表失败</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241612958.png" alt="image-20220924161249812" style="zoom:80%;"><h3 id="案例演示⭐" tabindex="-1">案例演示⭐ <a class="header-anchor" href="#案例演示⭐" aria-label="Permalink to &quot;案例演示⭐&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> b</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;"> b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-eq</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -eq </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> : a 等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -eq </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不等于 b</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># 输出这个</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-ne</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -ne </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不等于 b</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># 输出这个</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -ne </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> : a 等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-gt</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -gt </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -gt </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不大于 b</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># 输出这个</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-lt</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -lt </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 小于 b</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">   </span><span style="color:#676E95;font-style:italic;"># 输出这个</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -lt </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不小于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-ge</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -ge </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 大于或等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -ge </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 小于 b</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># 输出这个</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-le</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -le </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 小于或等于 b</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># 输出这个</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> -le </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">$a </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> $b</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &gt; </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &gt; </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">$a </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> $b</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &lt; </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 小于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &lt; </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不小于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">$a </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> $b</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &gt;= </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 大于或等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &gt;= </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 小于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">$a </span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;"> $b</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &lt;= </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 小于或等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &lt;= </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>运行效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241609977.png" alt="image-20220924160904836" style="zoom:80%;"><h2 id="字符串比较运算符" tabindex="-1">字符串比较运算符 <a class="header-anchor" href="#字符串比较运算符" aria-label="Permalink to &quot;字符串比较运算符&quot;">​</a></h2><blockquote><p>可以比较2个变量, 变量的类型可以为数字（整数，小数）与字符串</p></blockquote><h3 id="语法规则-5" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-5" aria-label="Permalink to &quot;语法规则&quot;">​</a></h3><p>下表列出了常用的字符串运算符，假定变量 a 为 &quot;abc&quot;，变量 b 为 &quot;efg&quot;：</p><p>字符串比较可以使用 <code>[[]]</code> 和 <code>[]</code> 2种方式</p><table><thead><tr><th style="text-align:left;">运算符</th><th style="text-align:left;">说明</th><th style="text-align:left;">举例</th></tr></thead><tbody><tr><td style="text-align:left;">== 或 =</td><td style="text-align:left;">相等。用于比较两个字符串或数字，相同则返回 0。可以使用<code>=</code></td><td style="text-align:left;"><code>[ $a == $b ] </code>返回1 <br><code>[ $a = $b ]</code> 返回 1<br><code>[[ $a == $b ]]</code> 返回1<br><code>[[ $a = $b ]]</code> 返回1</td></tr><tr><td style="text-align:left;">!=</td><td style="text-align:left;">不相等。用于比较两个字符串或数字，不相同则返回 0。</td><td style="text-align:left;"><code>[ $a != $b ]</code> 返回 0<br><code>[[ $a != $b ]]</code> 返回 0</td></tr><tr><td style="text-align:left;">&lt;</td><td style="text-align:left;">小于, 用于比较两个字符串或数字， 小于返回0， 否则返回1</td><td style="text-align:left;"><code>[ $a \&lt; $b ]</code> 返回 0<br><code>[[ $a &lt; $b ]]</code> 返回 0</td></tr><tr><td style="text-align:left;">&gt;</td><td style="text-align:left;">大于, 用于比较两个字符串或数字， 大于返回0， 否则返回1</td><td style="text-align:left;"><code>[ $a \&gt; $b ]</code> 返回 1<br><code>[[ $a &gt; $b ]]</code> 返回 1</td></tr><tr><td style="text-align:left;">-z</td><td style="text-align:left;">检测字符串长度是否为0，如果长度为0返回则返回0, 否则返回1。</td><td style="text-align:left;">[ -z $a ] 返回 false。</td></tr><tr><td style="text-align:left;">-n</td><td style="text-align:left;">检测字符串长度是否不为 0，如果长度不为 0 则返回0, 否则返回1。</td><td style="text-align:left;">[ -n &quot;$a&quot; ] 返回 true。</td></tr><tr><td style="text-align:left;">$</td><td style="text-align:left;">检测字符串是否不为空，不为空返回0, 为空返回1。</td><td style="text-align:left;">[ $a ] 返回 true。</td></tr></tbody></table><blockquote><p>字符串比较没有 <code>&lt;=</code> 可以通过 <code>[[ &quot;a&quot; &lt; &quot;b&quot; &amp;&amp; &quot;a&quot; == &quot;b&quot; ]]</code></p></blockquote><h3 id="实战演示-1" tabindex="-1">实战演示 <a class="header-anchor" href="#实战演示-1" aria-label="Permalink to &quot;实战演示&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> b</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itcast</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> c</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> d</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,c=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,d=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">d</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> = </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> : a 等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> = </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">!=</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> != </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> : a 不等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> != </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 等于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &gt; </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> : a 大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &gt; </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a \&gt; $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &gt; </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> : a 大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> &gt; </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;">: a 不大于 b</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> $c </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> $d </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$c</span><span style="color:#C3E88D;"> &gt; </span><span style="color:#A6ACCD;">$d</span><span style="color:#C3E88D;"> : c 大于 d</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$c</span><span style="color:#C3E88D;"> &gt; </span><span style="color:#A6ACCD;">$d</span><span style="color:#C3E88D;">: c 不大于 d</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-z</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">-z </span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> : 字符串长度为 0</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">-z </span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> : 字符串长度不为 0</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">-n </span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> : 字符串长度不为 0</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">-n </span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> : 字符串长度为 0</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> : 字符串不为空</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> : 字符串为空</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>运行效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241616215.png" alt="image-20220924161629079" style="zoom:80%;"><h2 id="布尔运算符" tabindex="-1">布尔运算符 <a class="header-anchor" href="#布尔运算符" aria-label="Permalink to &quot;布尔运算符&quot;">​</a></h2><h3 id="语法示例" tabindex="-1">语法示例 <a class="header-anchor" href="#语法示例" aria-label="Permalink to &quot;语法示例&quot;">​</a></h3><table><thead><tr><th style="text-align:left;">运算符</th><th style="text-align:left;">说明</th><th style="text-align:left;">举例</th></tr></thead><tbody><tr><td style="text-align:left;">!</td><td style="text-align:left;">非运算，取反, 表达式为 true 则返回 false，<br>否则返回 true。</td><td style="text-align:left;"><code>[ ! 表达式 ]</code> 取反。</td></tr><tr><td style="text-align:left;">-o</td><td style="text-align:left;">or 或运算，有一个表达式为 true 则返回 true。</td><td style="text-align:left;"><code>[ 表达式1 -o 表达式2 ]</code></td></tr><tr><td style="text-align:left;">-a</td><td style="text-align:left;">and 与运算，两个表达式都为 true 才返回 true。</td><td style="text-align:left;"><code>[ 表达式1 -a 表达式2 ]</code></td></tr></tbody></table><blockquote><p>注意布尔运算符放在<code>[]</code> 或 与test命令配合使用才有效</p><p>布尔运算符常与与test命令配合使用, 后续讲解</p></blockquote><h3 id="案例演示-4" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-4" aria-label="Permalink to &quot;案例演示&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> b</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># $a 小于 2 且 $b 大于 10</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-lt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-a</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">-gt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> 小于 2 且 </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> 大于 10 : 返回 true</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">   </span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> 小于 2 且 </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> 大于 10 : 返回 false</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># $b -gt 10不成立, 输出这个表达式</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># $a 小于 10 或 $b 大于 10</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-lt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-o</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">-gt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> 小于 10 或 </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> 大于 10 : 返回 true</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># $a -lt 10 成立, 输出这个表达式</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> 小于 10 或 </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> 大于 10 : 返回 false</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># $a 大于 $b 取反</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">!</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-gt</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> 大于 </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> 取反 : 返回 true</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$a</span><span style="color:#C3E88D;"> 大于 </span><span style="color:#A6ACCD;">$b</span><span style="color:#C3E88D;"> 取反 : 返回 false</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">   </span><span style="color:#676E95;font-style:italic;"># $a -gt $b 为true , 取反为false, 输出这个表达式</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241623736.png" alt="image-20220924162321619" style="zoom:80%;"><h2 id="逻辑运算符" tabindex="-1">逻辑运算符 <a class="header-anchor" href="#逻辑运算符" aria-label="Permalink to &quot;逻辑运算符&quot;">​</a></h2><h3 id="语法介绍" tabindex="-1">语法介绍 <a class="header-anchor" href="#语法介绍" aria-label="Permalink to &quot;语法介绍&quot;">​</a></h3><table><thead><tr><th style="text-align:left;">运算符</th><th style="text-align:left;">说明</th><th style="text-align:left;">举例</th></tr></thead><tbody><tr><td style="text-align:left;">&amp;&amp;</td><td style="text-align:left;">逻辑的 AND</td><td style="text-align:left;"><code>[[ 表达式1 &amp;&amp; 表达式2 ]]</code></td></tr><tr><td style="text-align:left;">`</td><td style="text-align:left;"></td><td style="text-align:left;">`</td></tr><tr><td style="text-align:left;">!</td><td style="text-align:left;">逻辑非</td><td style="text-align:left;"><code>[[ ! 表达式 ]]</code></td></tr></tbody></table><blockquote><p>使用<code>&amp;&amp;</code> 和 <code>||</code> 的运算符必须放在 <code>[[]]</code> 或 <code>(())</code>中才有效, 否则报错</p><p><code>!</code> 可以用在<code>[]</code>,<code>[[]]</code>中, 不可以在(())</p></blockquote><h3 id="案例演示-5" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-5" aria-label="Permalink to &quot;案例演示&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> b</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">2</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-lt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">-gt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">返回 true</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">返回 false</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># $b -gt 10 不成立, 输出false</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> $a </span><span style="color:#89DDFF;">-lt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">||</span><span style="color:#A6ACCD;"> $b </span><span style="color:#89DDFF;">-gt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">返回 true</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">   </span><span style="color:#676E95;font-style:italic;"># $a -lt 10 成立,  输出true</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">返回 false</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241625703.png" alt="image-20220924162527586" style="zoom:80%;"><h2 id="文件测试运算符⭐" tabindex="-1">文件测试运算符⭐ <a class="header-anchor" href="#文件测试运算符⭐" aria-label="Permalink to &quot;文件测试运算符⭐&quot;">​</a></h2><blockquote><p>能够使用常用文件测试运算符检查文件</p><p>例如: 文件是否存在\是否可读\是否可执行\是否为空\是否可写\是否是目录\是否是普通文件</p></blockquote><h3 id="文件类型" tabindex="-1">文件类型 <a class="header-anchor" href="#文件类型" aria-label="Permalink to &quot;文件类型&quot;">​</a></h3><blockquote><p>-：普通文件、d：目录文件、l：链接文件、b：块设备文件、c：字符设备文件、p：管道文件</p></blockquote><blockquote><p>块设备文件: 比如计算机硬盘/dev/sda</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119794.png" alt="image-20200708123349968"></p><p>字符设备文件: 比如计算机的USB文件/dev/usb</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241650819.png" alt="image-20220924165036700" style="zoom:80%;"><p>设备文件都是对应计算机硬件的, 不同的设备文件代表不同的传输数据方式</p></blockquote><h3 id="语法介绍-1" tabindex="-1">语法介绍 <a class="header-anchor" href="#语法介绍-1" aria-label="Permalink to &quot;语法介绍&quot;">​</a></h3><table><thead><tr><th style="text-align:left;">操作符</th><th style="text-align:left;">说明</th><th style="text-align:left;">举例</th></tr></thead><tbody><tr><td style="text-align:left;">-b file</td><td style="text-align:left;">检测文件是否是块设备文件，如果是，则返回 true。</td><td style="text-align:left;">[ -b $file ] 返回 false</td></tr><tr><td style="text-align:left;">-c file</td><td style="text-align:left;">检测文件是否是字符设备文件，如果是，则返回 true。</td><td style="text-align:left;">[ -c $file ] 返回 false</td></tr><tr><td style="text-align:left;">==-d file==</td><td style="text-align:left;">directory, 检测文件是否是目录，如果是，则返回 true。</td><td style="text-align:left;">[ -d $file ] 返回 false</td></tr><tr><td style="text-align:left;">==-f file==</td><td style="text-align:left;">file, 检测文件是否是普通文件（既不是目录，也不是设备文件），如果是，则返回 true。</td><td style="text-align:left;">[ -f $file ] 返回 true</td></tr><tr><td style="text-align:left;">-g file</td><td style="text-align:left;">检测文件是否设置了 SGID 位，如果是，则返回 true。</td><td style="text-align:left;">[ -g $file ] 返回 false</td></tr><tr><td style="text-align:left;">-k file</td><td style="text-align:left;">检测文件是否设置了粘着位(Sticky Bit)，如果是，则返回 true。</td><td style="text-align:left;">[ -k $file ] 返回 false</td></tr><tr><td style="text-align:left;">-p file</td><td style="text-align:left;">检测文件是否是有名管道文件，如果是，则返回 true。</td><td style="text-align:left;">[ -p $file ] 返回 false</td></tr><tr><td style="text-align:left;">-u file</td><td style="text-align:left;">检测文件是否设置了 SUID 位，如果是，则返回 true。</td><td style="text-align:left;">[ -u $file ] 返回 false</td></tr><tr><td style="text-align:left;">==-r file==</td><td style="text-align:left;">read,检测文件是否可读，如果是，则返回 true。</td><td style="text-align:left;">[ -r $file ] 返回 true</td></tr><tr><td style="text-align:left;">==-w file==</td><td style="text-align:left;">write,检测文件是否可写，如果是，则返回 true。</td><td style="text-align:left;">[ -w $file ] 返回 true</td></tr><tr><td style="text-align:left;">==-x file==</td><td style="text-align:left;">execute, 检测文件是否可执行，如果是，则返回 true。</td><td style="text-align:left;">[ -x $file ] 返回 true</td></tr><tr><td style="text-align:left;">==-s file==</td><td style="text-align:left;">size, 检测文件是否为空（文件大小是否大于0），不为空返回 true</td><td style="text-align:left;">[ -s $file ] 返回 true</td></tr><tr><td style="text-align:left;">==-e file==</td><td style="text-align:left;">exists, 检测文件（包括目录）是否存在，如果是，则返回 true。</td><td style="text-align:left;">[ -e $file ] 返回 true</td></tr><tr><td style="text-align:left;">file1 -nt file2</td><td style="text-align:left;">new than(nt), file1是否比file2新</td><td style="text-align:left;">[ file1 -nt file2 ]</td></tr><tr><td style="text-align:left;">file1 -ot file2</td><td style="text-align:left;">old than(ot), file1是否比file2旧</td><td style="text-align:left;">[ file1 -ot file2 ]</td></tr></tbody></table><p>其他检查符：</p><ul><li>-S: 判断某文件是否 socket。</li><li>-L: link, 检测文件是否存在并且是一个符号链接。</li></ul><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> options 文件路径字符串</span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#FFCB6B;">或</span></span>
<span class="line"><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> options 文件路径字符串 </span><span style="color:#89DDFF;">]]</span></span></code></pre></div><h3 id="案例演示-6" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-6" aria-label="Permalink to &quot;案例演示&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A6ACCD;">file1</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/root/docker-compose.yml</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">file2</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/root/net.sh</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 测试文件是否可写</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-w</span><span style="color:#A6ACCD;"> $file1 </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件可写</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件不可写</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 测试文件是否读</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-r</span><span style="color:#A6ACCD;"> $file1 </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件可读</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件不可读</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 测试文件是否可执行</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-x</span><span style="color:#A6ACCD;"> $file1 </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件可执行</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件不可执行</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 测试文件是否是普通文件</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-f</span><span style="color:#A6ACCD;"> $file1 </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件是普通文件</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件不是普通文件</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 测试文件是否为空</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-s</span><span style="color:#A6ACCD;"> $file1 </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件不为空</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件为空</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 测试文件是否存在</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-e</span><span style="color:#A6ACCD;"> $file1 </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件存在</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1文件不存在</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 测试文件是否是目录</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-d</span><span style="color:#A6ACCD;"> $file1 </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1是目录</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1不是目录</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 测试文件1是否比文件2新</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> $file1 </span><span style="color:#89DDFF;">-nt</span><span style="color:#A6ACCD;"> $file2 </span><span style="color:#89DDFF;">]]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1比file2新</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">file1比file2旧</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241647357.png" alt="image-20220924164736228" style="zoom:80%;"><h1 id="重要判断-执行符" tabindex="-1">重要判断&amp;执行符 <a class="header-anchor" href="#重要判断-执行符" aria-label="Permalink to &quot;重要判断&amp;执行符&quot;">​</a></h1><h2 id="" tabindex="-1">[[ ]] <a class="header-anchor" href="#" aria-label="Permalink to &quot;[[ ]]&quot;">​</a></h2><p><code>[[ ]]</code>: 是一个bash shell的关键字，用于测试条件表达式，支持高级表达式匹配和操作，例如<code>[[ $var == &quot;abc&quot; ]]</code>，比<code>[ $var = &quot;abc&quot; ]</code>更灵活。<code>[[ ]]</code>支持<code>&amp;&amp;</code>、<code>||</code>、<code>&lt;</code>、<code>&gt;</code>、<code>&lt;=</code>、<code>&gt;=</code>、<code>=~</code>等操作符，还支持文件名通配符和正则表达式等功能。在<code>[[ ]]</code>中使用变量时，不需要加引号，因为其中的变量扩展是不会被分割的。<code>[[ ]]</code>是bash shell中的扩展功能，不适用于其他的shell。</p><p>字符串比较</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$str1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">==</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$str2</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">str1 equals to str2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>模式匹配</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$str</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">==</span><span style="color:#A6ACCD;"> foo</span><span style="color:#89DDFF;">*</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">str starts with foo</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>文件名扩展</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-d</span><span style="color:#A6ACCD;"> /path/to/dir </span><span style="color:#89DDFF;">]];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/path/to/dir exists and is a directory</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>正则表达式匹配</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$str</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=~</span><span style="color:#A6ACCD;"> pattern </span><span style="color:#89DDFF;">]];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">str matches pattern</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>数值比较</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-eq</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num2</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">num1 equals to num2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><h2 id="-1" tabindex="-1">[ ] <a class="header-anchor" href="#-1" aria-label="Permalink to &quot;[ ]&quot;">​</a></h2><p><code>[]</code>: 是一种常用的条件测试命令，常用于if语句中进行条件测试。例如<code>[ $var -eq 5 ]</code>，表示判断变量<code>$var</code>的值是否等于5。<code>[]</code>支持各种比较运算符和逻辑运算符，例如<code>-eq</code>、<code>-ne</code>、<code>-lt</code>、<code>-gt</code>、<code>-le</code>、<code>-ge</code>、<code>&amp;&amp;</code>、<code>||</code>等。需要注意的是，在<code>[]</code>中使用变量时，需要将变量用引号引起来，以避免空格和其他特殊字符对测试结果的影响。</p><p>字符串比较</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$str1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$str2</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">str1 equals to str2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>数值比较</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-eq</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num2</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">num1 equals to num2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>文件检查</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-f</span><span style="color:#A6ACCD;"> /path/to/file </span><span style="color:#89DDFF;">];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/path/to/file exists and is a regular file</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>目录检查</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-d</span><span style="color:#A6ACCD;"> /path/to/dir </span><span style="color:#89DDFF;">];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">/path/to/dir exists and is a directory</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>注意，在使用<code>[]</code>时，需要使用空格将不同的测试项分开，并将变量使用双引号引起来，否则会出现意料之外的错误</p><h2 id="-2" tabindex="-1">( ) <a class="header-anchor" href="#-2" aria-label="Permalink to &quot;( )&quot;">​</a></h2><p><code>()</code>: 是用于创建子shell的符号，括号中的命令将在一个子shell中执行，该子shell中的变量和环境变量与当前shell是独立的。例如，<code>(cd /tmp; ls)</code>表示在一个子shell中执行<code>cd /tmp</code>和<code>ls</code>命令。</p><p>创建子Shell（subshell）</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 在子Shell中运行命令</span></span>
<span class="line"><span style="color:#89DDFF;">(</span><span style="color:#82AAFF;">cd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">ls</span><span style="color:#89DDFF;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 在子Shell中定义变量</span></span>
<span class="line"><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">var</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">value</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $var</span><span style="color:#89DDFF;">)</span></span></code></pre></div><p>定义数组</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 定义索引数组</span></span>
<span class="line"><span style="color:#A6ACCD;">arr</span><span style="color:#89DDFF;">=(</span><span style="color:#C3E88D;">a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">b</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">c</span><span style="color:#89DDFF;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 定义关联数组</span></span>
<span class="line"><span style="color:#C792EA;">declare</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-A</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">arr=</span><span style="color:#A6ACCD;">(</span><span style="color:#C3E88D;">[name]=John</span><span style="color:#A6ACCD;"> [age]=30)</span></span></code></pre></div><p>需要注意的是，使用<code>()</code>时，不需要在括号周围添加空格。</p><h2 id="-3" tabindex="-1">(( )) <a class="header-anchor" href="#-3" aria-label="Permalink to &quot;(( ))&quot;">​</a></h2><p><code>(())</code>: 是一种用于进行算术运算的符号，例如<code>((x=5+3))</code>表示将8赋值给变量<code>$x</code>。<code>(())</code>支持算术运算符和逻辑运算符，例如<code>+</code>、<code>-</code>、<code>*</code>、<code>/</code>、<code>%</code>、<code>&amp;</code>、<code>|</code>、<code>^</code>、<code>~</code>等。</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 定义变量并进行整数运算</span></span>
<span class="line"><span style="color:#A6ACCD;">num1</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">10</span></span>
<span class="line"><span style="color:#A6ACCD;">num2</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">20</span></span>
<span class="line"><span style="color:#A6ACCD;">result</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$((</span><span style="color:#C3E88D;">num</span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">+</span><span style="color:#C3E88D;"> num</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">))</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 支持常见的数值运算符</span></span>
<span class="line"><span style="color:#A6ACCD;">result</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$((</span><span style="color:#C3E88D;">num</span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">+</span><span style="color:#C3E88D;"> num</span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">*</span><span style="color:#C3E88D;"> </span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">))</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 支持变量的自增和自减</span></span>
<span class="line"><span style="color:#A6ACCD;">num1</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">10</span></span>
<span class="line"><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">num</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">++))</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $num1  </span><span style="color:#676E95;font-style:italic;"># 输出 11</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">num</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">--))</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $num1  </span><span style="color:#676E95;font-style:italic;"># 输出 10</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 支持常见的位运算符</span></span>
<span class="line"><span style="color:#A6ACCD;">result</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$((</span><span style="color:#C3E88D;">num</span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&amp;</span><span style="color:#C3E88D;"> num</span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">))</span></span></code></pre></div><h2 id="test" tabindex="-1">test <a class="header-anchor" href="#test" aria-label="Permalink to &quot;test&quot;">​</a></h2><blockquote><p><strong>test 命令用于检查某个条件是否成立，它可以进行数值、字符和文件三个方面的测试</strong>。<strong>功能与[]一样</strong>，可以用于if语句中进行条件测试。例如，<code>test -f /etc/passwd</code>表示测试文件<code>/etc/passwd</code>是否存在。<code>test</code>命令支持各种比较运算符和逻辑运算符，例如<code>-eq</code>、<code>-ne</code>、<code>-lt</code>、<code>-gt</code>、<code>-le</code>、<code>-ge</code>、<code>&amp;&amp;</code>、<code>||</code>等。</p></blockquote><h3 id="基本语法-3" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-3" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">数字1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">options</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">数字2</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;">  </span></span>
<span class="line"><span style="color:#82AAFF;">...</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>options具体如下</p><table><thead><tr><th style="text-align:left;">参数</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">-eq</td><td style="text-align:left;">等于则为真</td></tr><tr><td style="text-align:left;">-ne</td><td style="text-align:left;">不等于则为真</td></tr><tr><td style="text-align:left;">-gt</td><td style="text-align:left;">大于则为真</td></tr><tr><td style="text-align:left;">-ge</td><td style="text-align:left;">大于等于则为真</td></tr><tr><td style="text-align:left;">-lt</td><td style="text-align:left;">小于则为真</td></tr><tr><td style="text-align:left;">-le</td><td style="text-align:left;">小于等于则为真</td></tr></tbody></table><h3 id="使用实例" tabindex="-1">使用实例 <a class="header-anchor" href="#使用实例" aria-label="Permalink to &quot;使用实例&quot;">​</a></h3><p>检查文件是否存在</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/path/to/file</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">文件存在</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">文件不存在</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>检查目录是否存在</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/path/to/dir</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">目录存在</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">目录不存在</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>比较两个字符串</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$str1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">=</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$str2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">两个字符串相等</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">两个字符串不相等</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-z</span><span style="color:#A6ACCD;"> $str2</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">str2字符串长度为0</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">str2字符串长度不为0</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>检查数字大小关系</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-eq</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">两个数字相等</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">elif</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-lt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num1</span><span style="color:#C3E88D;"> 小于 </span><span style="color:#A6ACCD;">$num2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$num1</span><span style="color:#C3E88D;"> 大于 </span><span style="color:#A6ACCD;">$num2</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><h2 id="和-的区别" tabindex="-1">[[]] 和 [] 的区别 <a class="header-anchor" href="#和-的区别" aria-label="Permalink to &quot;[[]] 和 [] 的区别&quot;">​</a></h2><h4 id="区别1-word-splitting的发生" tabindex="-1">区别1: word splitting的发生 <a class="header-anchor" href="#区别1-word-splitting的发生" aria-label="Permalink to &quot;区别1: word splitting的发生&quot;">​</a></h4><p><code>[[]]</code> 不会有word splitting发生</p><p><code>[]</code> 会有word splitting发生</p><blockquote><p>会将含有空格字符串进行分拆分割后比较</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119060.png" alt="image-20200704092823470" style="zoom:80%;"><blockquote><p>通过 <code>$?</code> 获取上一个命令的退出状态, 0代表成功, 1代表失败</p></blockquote><h4 id="区别2-转义字符" tabindex="-1">区别2: 转义字符 <a class="header-anchor" href="#区别2-转义字符" aria-label="Permalink to &quot;区别2: 转义字符&quot;">​</a></h4><p><code>[[]]</code> 对 <code>&lt;</code> 不需要转义, 格式为 <code> [[ 字符串1 &lt; 字符串2 ]]</code></p><p><code>[]</code> 需要对 <code>&lt;,&gt;</code> 转义 , 格式为 <code> [ 字符串1 \&lt; 字符串2 ]</code></p><p><code>[]</code> 执行效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119065.png" alt="image-20200704100701687" style="zoom:80%;"><p><code>[[]]</code> 执行效果, 不需要转义执行结果正确</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042119068.png" alt="image-20200704100843230" style="zoom:80%;"><h1 id="计算命令" tabindex="-1">计算命令 <a class="header-anchor" href="#计算命令" aria-label="Permalink to &quot;计算命令&quot;">​</a></h1><h2 id="expr命令详解" tabindex="-1">expr命令详解 <a class="header-anchor" href="#expr命令详解" aria-label="Permalink to &quot;expr命令详解&quot;">​</a></h2><blockquote><p>expr (evaluate expressions 的缩写)，译为“表达式求值”。是一个功能强大，并且比较复杂的命令，它除了可以实现整数计算，还可以结合一些选项对字符串进行处理，例如计算字符串长度、字符串比较、字符串匹配、字符串提取等。优点: 可以直接输出，缺点: 计算表达式里面引用变量使用$, 特殊字符需要转义，只能计算一个表达式</p></blockquote><blockquote><ol><li><p>整数计算</p><p><code>expr 整数运算表达式</code></p></li><li><p>字符串操作</p><p><code>expr length 字符串</code> 获取字符串的长度</p><p><code>expr substr 字符串 start end</code> 截取字符串</p><p><code>expr index 字符串 查找的字符</code> 查找字符在字符串中第一次出现的位置, 位置从1开始的</p><p><code>expr match 字符串 正则表达式</code></p><p><code>expr 字符串: 正则表达式</code></p></li></ol></blockquote><h3 id="求值表达式-在上面有" tabindex="-1">求值表达式(在上面有) <a class="header-anchor" href="#求值表达式-在上面有" aria-label="Permalink to &quot;求值表达式(在上面有)&quot;">​</a></h3><p>计算语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">算术运算符表达式</span></span>
<span class="line"><span style="color:#FFCB6B;">例如:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">+</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">返回:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#FFCB6B;">例如:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">expr</span><span style="color:#A6ACCD;"> \( </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">+</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> \) \* </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">+</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">返回:140</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120617.png" alt="image-20200703102043699"></p><p>获取计算结果赋值给新变量语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">result</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> 算术运算符表达式</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#A6ACCD;">result</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;"> + </span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">  </span><span style="color:#FFCB6B;">输出result得到结果:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span></span></code></pre></div><h3 id="字符串api" tabindex="-1">字符串API <a class="header-anchor" href="#字符串api" aria-label="Permalink to &quot;字符串API&quot;">​</a></h3><p>计算字符串的长度语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">length</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">字符串</span></span>
<span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">length</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">返回:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">7</span></span></code></pre></div><p>截取字符串语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">substr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">字符串</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">start</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">end</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># start 截取字符串的起始位置, 从1开始</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># end 截取字符串的结束位置, 包含这个位置截取</span></span>
<span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">substr</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">返回:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">it</span></span></code></pre></div><p>获取第一个字符在字符串中出现的位置语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">index</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">被查找字符串</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">需要查找的字符</span></span>
<span class="line"><span style="color:#FFCB6B;">例如</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">index</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">t</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">会返回:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span></span></code></pre></div><p>正则表达式匹配1语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">match</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">字符串</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">正则表达式</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 正则表达式默认带有^ ,  代表以什么开头</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 返回值为符合匹配字符的长度, 否则返回为0</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 例如: expr match &quot;itheima&quot; &quot;.*m&quot;  会返回: 6</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 正则表达式通配符&quot;.&quot;代表任意一个字符</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 正则表达式通配符&quot;*&quot;代表签名的字符可以出现0到多次</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &quot;.*m&quot; 含义为匹配字符串中m前面的字符串长度</span></span></code></pre></div><p>正则表表达式匹配2语法, 功能与语法1一样</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">字符串</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">正则表达式</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 正则表达式默认带有^ ,  代表以什么开头，.*表示0到多个，.*m匹配0到多个m</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 返回值为符合匹配字符的长度, 否则返回为0</span></span>
<span class="line"><span style="color:#FFCB6B;">expr</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">.*m</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">返回:</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">6</span></span></code></pre></div><h3 id="实战演练⭐-2" tabindex="-1">实战演练⭐ <a class="header-anchor" href="#实战演练⭐-2" aria-label="Permalink to &quot;实战演练⭐&quot;">​</a></h3><blockquote><p>四则运算: ( 10 + 10 ) * 2 + 100</p><p>计算&quot;itheima&quot;字符串的长度</p><p>获取&quot;itheima&quot;字符串中&quot;t&quot;第一个字符在字符串中出现的位置</p><p>正则表达式match匹配查找itheima字符串中m前面任意字符的总长度</p></blockquote><p>cal1.sh脚本文件代码</p><div class="language-Shell"><button title="Copy Code" class="copy"></button><span class="lang">Shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 四则运算</span></span>
<span class="line"><span style="color:#A6ACCD;">result</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">\(</span><span style="color:#C3E88D;"> </span><span style="color:#F78C6C;">10</span><span style="color:#C3E88D;"> + </span><span style="color:#F78C6C;">10</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">\)</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">\*</span><span style="color:#C3E88D;"> </span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;"> + </span><span style="color:#F78C6C;">100</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">(10+10)*2+100=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">result</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算字符串的长度</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima字符串长度=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> length </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;`&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 获取第一个字符在字符串中出现的位置</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima字符串中第一个t的位置=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> index </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> t</span><span style="color:#89DDFF;">`&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 正则表达式匹配1</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">正则表达式match匹配查找itheima字符串中m前面任意字符的总长度=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> match </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">.*m</span><span style="color:#89DDFF;">&quot;`&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 正则表达式匹配2</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">正则表达式匹配查找itheima字符串中m前面任意字符的总长度=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">expr</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> : </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">.*m</span><span style="color:#89DDFF;">&quot;`&quot;</span></span></code></pre></div><p>运行效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241700789.png" alt="image-20220924170056648" style="zoom:80%;"><h2 id="命令详解" tabindex="-1">(())命令详解 <a class="header-anchor" href="#命令详解" aria-label="Permalink to &quot;(())命令详解&quot;">​</a></h2><h3 id="语法分析" tabindex="-1">语法分析 <a class="header-anchor" href="#语法分析" aria-label="Permalink to &quot;语法分析&quot;">​</a></h3><blockquote><p>双小括号 (( )) , 用于进行数学运算表达式的执行 , 将数学运算表达式放在<code>((</code>和<code>))</code>之间。</p><p>可以使用<code>$</code>获取 (( )) 表达式命令的结果，这和使用<code>$</code>获得变量值是一样的。</p><p>优点: 直接输出, 里面直接使用变量名, 特殊字符不需要转义, 多个表达式赋值</p><p>缺点: 需要获取值以后才可以输出</p></blockquote><blockquote><p>括号内赋值: <strong>((变量名=整数表达式))</strong></p><p>括号外赋值: <strong>变量名=$((整数表达式))</strong></p><p>多表达式赋值: <strong>((变量名1=整数表达式1,变量名2=整数表达式2,...))</strong></p><p>与if条件句配合使用: <strong>if ((整数表达式))</strong></p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;">(</span><span style="color:#A6ACCD;">(表达式</span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;">)</span></span></code></pre></div><table><thead><tr><th>运算操作符/运算命令</th><th>说明</th></tr></thead><tbody><tr><td>((a=1+6))<br> ((b=a-1)) <br>((c=a+b))</td><td>这种写法可以在计算完成后给变量赋值。以 ((b=a-1)) 为例，<br>即将 a-1 的运算结果赋值给变量 c。 注意，使用变量时不用加<code>$</code>前缀，<br>(( )) 会自动解析变量名。</td></tr><tr><td><code>a=$((1+6)</code><br> <code>b=$((a-1))</code><br> <code>c=$((a+b))</code></td><td>可以在 (( )) 前面加上<code>$</code>符号获取 (( )) 命令的执行结果，<br>也即获取整个表达式的值。以 <code>c=$((a+b))</code> 为例，即将 a+b 这个<br>表达式的运算结果赋值给变量 c。 注意，如果 c=((a+b)) 这样的写<br>法是错误的，不加<code>$</code>就不能取得表达式的结果。</td></tr><tr><td>((a&gt;7 &amp;&amp; b==c))</td><td>(( )) 也可以进行逻辑运算，在 if 语句中常会使用逻辑运算。</td></tr><tr><td>echo $((a+10))</td><td>需要立即输出表达式的运算结果时，可以在 (( )) 前面加<code>$</code>符号。</td></tr><tr><td>((a=3+5, b=a+10))</td><td>对多个表达式同时进行计算, 多表表达式使用&quot;,&quot;号隔开</td></tr></tbody></table><blockquote><p>注意: 符号之间有无空格都可以 , (( a = 1 + 6 )) 等价于 ((a=1+6))</p></blockquote><h3 id="案例演示-7" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-7" aria-label="Permalink to &quot;案例演示&quot;">​</a></h3><p>calculatej2.sh脚本代码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算1+6赋值给变量a</span></span>
<span class="line"><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">6</span><span style="color:#89DDFF;">))</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算变量a-1赋值给变量b</span></span>
<span class="line"><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">))</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算变量a+变量b赋值给变量c</span></span>
<span class="line"><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">))</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印变量a,变量b, 变量c的值</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,c=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># $赋值写法</span></span>
<span class="line"><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$((</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">6</span><span style="color:#89DDFF;">))</span><span style="color:#A6ACCD;"> b</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$((</span><span style="color:#C3E88D;">a</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">))</span><span style="color:#A6ACCD;"> c</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$((</span><span style="color:#C3E88D;">a</span><span style="color:#89DDFF;">+</span><span style="color:#C3E88D;">b</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,c=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># (())多个表达式计算赋值</span></span>
<span class="line"><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">6</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">-</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">,</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,c=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># echo输出直接使用(())</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">1+6=</span><span style="color:#89DDFF;">$((</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">6</span><span style="color:#89DDFF;">))&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># (()) 用于逻辑表达式 在if中使用</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">&gt;</span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> b</span><span style="color:#89DDFF;">==</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a&gt;7 &amp;&amp; b==c 成立</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a&gt;7 &amp;&amp; b==c 不成立</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241710307.png" alt="image-20220924171049178" style="zoom:80%;"><h2 id="let命令详解" tabindex="-1">let命令详解 <a class="header-anchor" href="#let命令详解" aria-label="Permalink to &quot;let命令详解&quot;">​</a></h2><blockquote><p>let 命令和双小括号 (( )) 在数字计算方面功能一样. 但是没有(())功能强大, <strong>let只能用于赋值计算, 不能直接输出, 不可以条件判断一起使用</strong></p><p>优点: 赋值简单,特殊字符不需要转义。缺点: 不能直接输出</p></blockquote><p>作用: 用于赋值,是最简洁的整数运算赋值命令</p><blockquote><p>计算赋值用法: let 变量名=整数运算表达式</p><p>多个表达式计算赋值用法: let 变量名1=整数运算表达式1 变量名2=整数运算表达式2 ...</p></blockquote><h3 id="语法规则-6" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-6" aria-label="Permalink to &quot;语法规则&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">let</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">赋值表达式</span></span></code></pre></div><blockquote><p>注意</p><ol><li><p>语法功能等价于<code>((表达式))</code></p></li><li><p>多个表达式之间使用空格, 不是&quot;,&quot;号</p></li><li><p>对于类似<code>let a+b</code>这样的写法，Shell 虽然计算了 a+b 的值，但却将结果丢弃, 如果 <code>echo let a+b</code> 会直接输出字符串 <code>a+b</code> ；若不想这样，可以使用<code>let sum=a+b</code>将 a+b 的结果保存在变量 sum 中。</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120624.png" alt="image-20200703113800668"></p><p>输出建议使用(())</p></li></ol></blockquote><h3 id="实战演练-1" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练-1" aria-label="Permalink to &quot;实战演练&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算1+6赋值给变量a</span></span>
<span class="line"><span style="color:#82AAFF;">let</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">a=</span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">+6</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算变量a-1赋值给变量b</span></span>
<span class="line"><span style="color:#82AAFF;">let</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">b=a-1</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算变量a+变量b赋值给变量c</span></span>
<span class="line"><span style="color:#82AAFF;">let</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">c=a+b</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印变量a,变量b, 变量c的值</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,c=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># let多个表达式计算赋值</span></span>
<span class="line"><span style="color:#82AAFF;">let</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">a=</span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">+6</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">b=a-1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">c=a+b</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,c=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">}&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241714003.png" alt="image-20220924171420850" style="zoom:80%;"><h2 id="命令详解-1" tabindex="-1">$[]命令详解 <a class="header-anchor" href="#命令详解-1" aria-label="Permalink to &quot;$[]命令详解&quot;">​</a></h2><blockquote><p>和 (())、let 命令类似，$[] 也只能进行整数运算。但是只能对单个表达式的计算求值与输出：$[表达式]</p><p>优点: 特殊字符不需要转义,</p><p>缺点: 不能多表达是计算,</p></blockquote><blockquote><ol><li><p>$[] 会对<code>表达式</code>进行计算，并取得计算结果</p></li><li><p>表达式内部不可以赋值给变量</p></li></ol></blockquote><p>calculate4.sh脚本代码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算1+6赋值给变量a</span></span>
<span class="line"><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">$</span><span style="color:#C3E88D;">[</span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">+6]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算变量a-1赋值给变量b</span></span>
<span class="line"><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">$</span><span style="color:#C3E88D;">[a-1]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 计算变量a+变量b赋值给变量c</span></span>
<span class="line"><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">$</span><span style="color:#C3E88D;">[a+b]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 打印变量a,变量b, 变量c的值</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">a=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">a</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,b=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">b</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">,c=</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">c</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 直接输出</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">$[1+6],$[7-1],$[7+6]</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241716331.png" alt="image-20220924171635207" style="zoom:80%;"><h2 id="bc命令详解" tabindex="-1">bc命令详解 <a class="header-anchor" href="#bc命令详解" aria-label="Permalink to &quot;bc命令详解&quot;">​</a></h2><blockquote><p>Bash shell内置了对整数运算的支持，但是并不支持浮点运算，而 linux bc (basic calculator)命令可以很方便的进行浮点运算. bc命令是Linux简单的计算器,能进行进制转换与计算。能转换的进制包括十六进制、十进制、八进制、二进制等。可以使用的运算符号包括(+)加法、(-)减法、(*)乘法、(/)除法、(^)指数、(%)余数等</p></blockquote><p>通常在linux下bc当计算器用, 具体有3个用法</p><ol><li>bc中互动式的数学运算</li><li>shell中非互动式的管道运算</li><li>shell中非互动式的输入重定向运算</li></ol><h3 id="语法分析-1" tabindex="-1">语法分析 <a class="header-anchor" href="#语法分析-1" aria-label="Permalink to &quot;语法分析&quot;">​</a></h3><div class="language-shelll"><button title="Copy Code" class="copy"></button><span class="lang">shelll</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">bc [options] [参数]</span></span></code></pre></div><table><thead><tr><th>选项</th><th>说明</th></tr></thead><tbody><tr><td>-h</td><td>help，帮助信息</td></tr><tr><td>-v</td><td>version，显示命令版本信息</td></tr><tr><td>==-l==</td><td>mathlib, 使用标准数学库, 例如使用内置函数就需要使用这个参数</td></tr><tr><td>-i</td><td>interactive, 强制交互</td></tr><tr><td>-w</td><td>warn, 显示 POSIX 的警告信息</td></tr><tr><td>-s</td><td>standard, 使用 POSIX 标准来处理</td></tr><tr><td>==-q==</td><td>quiet, 不显示欢迎信息</td></tr></tbody></table><blockquote><p>默认使用bc命令后回车会有很多欢迎信息， 可以使用 <code>bc -q</code> 回车后不会有欢迎信息</p></blockquote><p>示例演示</p><p>创建task.txt文件, 编辑文件内容(一个计算表达式一行)</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">108*67+12345</span></span>
<span class="line"><span style="color:#FFCB6B;">58+2007*11</span></span></code></pre></div><p>执行命令</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120507.png" alt="image-20200703153959498"></p><blockquote><p>可以使用 <code>quit</code> 命令退出bc</p></blockquote><h3 id="内置变量" tabindex="-1">内置变量 <a class="header-anchor" href="#内置变量" aria-label="Permalink to &quot;内置变量&quot;">​</a></h3><table><thead><tr><th>变量名</th><th>作 用</th></tr></thead><tbody><tr><td>scale</td><td>指定精度，也即小数点后的位数, 对计算结果指定保留小数；默认为 0，也即不使用小数部分。</td></tr><tr><td>ibase</td><td>指定输入的数字的进制，默认为十进制。</td></tr><tr><td>obase</td><td>指定输出的数字的进制，默认为十进制。</td></tr><tr><td>last 或者 .</td><td>获取最近计算打印结果的数字</td></tr></tbody></table><h3 id="内置数学函数" tabindex="-1">内置数学函数 <a class="header-anchor" href="#内置数学函数" aria-label="Permalink to &quot;内置数学函数&quot;">​</a></h3><table><thead><tr><th>函数名</th><th>作用</th></tr></thead><tbody><tr><td>s(x)</td><td>计算 x 的正弦值，x 是弧度值。</td></tr><tr><td>c(x)</td><td>计算 x 的余弦值，x 是弧度值。</td></tr><tr><td>a(x)</td><td>计算 x 的反正切值，返回弧度值。</td></tr><tr><td>l(x)</td><td>计算 x 的自然对数。</td></tr><tr><td>e(x)</td><td>求 e 的 x 次方。</td></tr><tr><td>j(n, x)</td><td>贝塞尔函数，计算从 n 到 x 的阶数。</td></tr></tbody></table><h3 id="示例-bc中互动式的数学运算" tabindex="-1">示例：bc中互动式的数学运算 <a class="header-anchor" href="#示例-bc中互动式的数学运算" aria-label="Permalink to &quot;示例：bc中互动式的数学运算&quot;">​</a></h3><p>使用 <code>bc -q</code> 命令，回车即可, 直接进行计算器进行运算</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120509.png" alt="image-20200703151035148" style="zoom:80%;"><p>退出bc使用 <code>quit</code></p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120517.png" alt="image-20200703151349964" style="zoom:80%;"><h3 id="示例-shell中非互动式的管道运算" tabindex="-1">示例：shell中非互动式的管道运算 <a class="header-anchor" href="#示例-shell中非互动式的管道运算" aria-label="Permalink to &quot;示例：shell中非互动式的管道运算&quot;">​</a></h3><p>在 Shell 脚本中，我们可以借助管道使用 bc 计算器</p><p>借助管道使用 bc 计算器语法</p><p>直接进行bc的表达式计算输出</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">expression</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">bc</span><span style="color:#A6ACCD;"> [options]</span></span></code></pre></div><blockquote><p>&quot;expression&quot; 表达式必须复合bc命令要求的公式</p><p>&quot;expression&quot; 表达式里面可以引用shell变量</p><p>例如: Shell变量 <code>a=2</code> 在表达式里面引用的语法: <code>$a</code></p></blockquote><p>将bc计算结果赋值给Shell变量</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 第一种方式</span></span>
<span class="line"><span style="color:#A6ACCD;">var_name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#82AAFF;">echo</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">expression</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">|</span><span style="color:#C3E88D;"> </span><span style="color:#FFCB6B;">bc</span><span style="color:#C3E88D;"> [options]</span><span style="color:#89DDFF;">`</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 第二种方式</span></span>
<span class="line"><span style="color:#A6ACCD;">var_name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$(</span><span style="color:#82AAFF;">echo</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">expression</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">|</span><span style="color:#C3E88D;"> </span><span style="color:#FFCB6B;">bc</span><span style="color:#C3E88D;"> [options]</span><span style="color:#89DDFF;">)</span></span></code></pre></div><blockquote><p>$() 与 `` 功能一样, 都是执行里面的命令</p><p>区别</p><p>​ `` 是所有linux系统支持的方式, 兼容性较好, 但是容易与引号产生混淆</p><p>​ $() 不是所有linux系统都支持的方式, 兼容性较差, 但是不容易产生混淆</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120522.png" alt="image-20200703154646821"></p><p>引用shell变量进行计算</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120524.png" alt="image-20200703155152427"></p><blockquote><p>注意 b是bc中定义的新变量, 与shell变量没有任何关系, 所以不可以在shell中获取b变量</p></blockquote><p>进制转换</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120527.png" alt="image-20200703155732347"></p><h3 id="示例-shell中非互动式的输入重定向运算" tabindex="-1">示例：shell中非互动式的输入重定向运算 <a class="header-anchor" href="#示例-shell中非互动式的输入重定向运算" aria-label="Permalink to &quot;示例：shell中非互动式的输入重定向运算&quot;">​</a></h3><blockquote><p>将计算表达式输出给bc去执行, 特点类似于文件中输入,可以输入多行表达式, 更加清晰</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 第一种方式</span></span>
<span class="line"><span style="color:#A6ACCD;">var_name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">bc</span><span style="color:#C3E88D;"> [options] </span><span style="color:#89DDFF;">&lt;&lt;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">EOF</span></span>
<span class="line"><span style="color:#C3E88D;">第一行表达式1</span></span>
<span class="line"><span style="color:#C3E88D;">第二行表达式2</span></span>
<span class="line"><span style="color:#C3E88D;">...</span></span>
<span class="line"><span style="color:#89DDFF;">EOF</span></span>
<span class="line"><span style="color:#89DDFF;">`</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 第二种方式</span></span>
<span class="line"><span style="color:#A6ACCD;">var_name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$(</span><span style="color:#FFCB6B;">bc</span><span style="color:#C3E88D;"> [options] </span><span style="color:#89DDFF;">&lt;&lt;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">EOF</span></span>
<span class="line"><span style="color:#C3E88D;">第一行表达式1</span></span>
<span class="line"><span style="color:#C3E88D;">第二行表达式2</span></span>
<span class="line"><span style="color:#C3E88D;">...</span></span>
<span class="line"><span style="color:#89DDFF;">EOF</span></span>
<span class="line"><span style="color:#89DDFF;">)</span></span></code></pre></div><blockquote><p><code>var_name</code> 这是Shell变量的名字</p><p><code>bc</code> 执行bc的命令</p><p><code>EOF..EOF</code> 输入流的多行表达式</p><p>含义: 将EOF中间多行表达式输入给到bc去执行, j将bc执行的结果给到Shell变量var_name</p></blockquote><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120113.png" alt="image-20200703161628360"></p><blockquote><p>如果有大量的数学计算，那么使用输入重定向就比较方便，因为数学表达式可以换行，写起来更加清晰。</p></blockquote><h1 id="流程控制" tabindex="-1">流程控制 <a class="header-anchor" href="#流程控制" aria-label="Permalink to &quot;流程控制&quot;">​</a></h1><h2 id="if-else语句" tabindex="-1">if else语句 <a class="header-anchor" href="#if-else语句" aria-label="Permalink to &quot;if else语句&quot;">​</a></h2><blockquote><p>if条件判断逻辑控制语句</p></blockquote><h3 id="if语法" tabindex="-1">if语法 <a class="header-anchor" href="#if语法" aria-label="Permalink to &quot;if语法&quot;">​</a></h3><p>多行写法语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;">  </span><span style="color:#FFCB6B;">条件</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>可以将if语句放入一行语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">条件</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">命令</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><h3 id="if-else-语法" tabindex="-1">if else 语法 <a class="header-anchor" href="#if-else-语法" aria-label="Permalink to &quot;if else 语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;">  </span><span style="color:#FFCB6B;">条件</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#FFCB6B;">命令</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#FFCB6B;">命令</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><h3 id="if-elif-else-语法" tabindex="-1">if elif else 语法 <a class="header-anchor" href="#if-elif-else-语法" aria-label="Permalink to &quot;if elif else 语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;">  </span><span style="color:#FFCB6B;">条件1</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#FFCB6B;">命令1</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">elif</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">条件2</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令2</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">elif</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">条件3</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令3</span></span>
<span class="line"><span style="color:#FFCB6B;">……</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#FFCB6B;">命令N</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><h3 id="实战演练-2" tabindex="-1">实战演练 <a class="header-anchor" href="#实战演练-2" aria-label="Permalink to &quot;实战演练&quot;">​</a></h3><blockquote><p>提示&quot;请输入你的考试成绩:&quot; 接收用户输入一个成绩, 之后使用if else条件句判断</p><p>要求1: 小于 60 输出&quot;不及格&quot;</p><p>要求2: 大于等于70 并且 小于80 输出&quot;中等&quot;</p><p>要求3: 大于等于80 并且 小于90 输出&quot;良好&quot;</p><p>要求4: 大于等于90 并且 小于等于100 输出&quot;优秀&quot;</p><p>要求5: 以上不符合输出&quot;成绩不合法&quot;</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -p：显示提示信息</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入你的考试成绩:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">score</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">不及格</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">elif</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">70</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">及格</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">elif</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">70</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">80</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">中等</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">elif</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">80</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">90</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">良好</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">elif</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&gt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">90</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> $score </span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">优秀</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">成绩不合法</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241808079.png" alt="image-20220924180838933" style="zoom:80%;"><h2 id="if退出状态" tabindex="-1">if退出状态 <a class="header-anchor" href="#if退出状态" aria-label="Permalink to &quot;if退出状态&quot;">​</a></h2><blockquote><p>linux任何命令的的执行都会有一个退出状态, 无论是内置命令还是外部文件命令. 还是自定义的 Shell 函数，当它退出（运行结束）时，都会返回一个比较小的整数值给调用（使用）它的程序，这就是命令的<strong>退出状态</strong></p></blockquote><blockquote><p>查询每个命令的退出状态：常见命令 <code>$?</code>。大多数命令状态0代表成功, 非0代表失败. 也有特殊的命令,比如 diff 命令用来比较两个文件的不同，对于“没有差别”的文件返回 0，对于“找到差别”的文件返回 1，对无效文件名返回 2</p></blockquote><p>Shell 中，有多种方式取得命令的退出状态，其中 <code>$?</code> 是最常见的一种.</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120129.png" alt="image-20200703172613295"></p><h3 id="和逻辑符组合" tabindex="-1">和逻辑符组合 <a class="header-anchor" href="#和逻辑符组合" aria-label="Permalink to &quot;和逻辑符组合&quot;">​</a></h3><p>Shell if 语句使用逻辑运算符将多个退出状态组合起来，这样就可以一次判断多个条件了。</p><table><thead><tr><th>运算符</th><th>使用格式</th><th>说明</th></tr></thead><tbody><tr><td>&amp;&amp; 或 -a</td><td>条件1 &amp;&amp; 条件2</td><td>逻辑与运算符，当 条件1 和 条件2 同时成立时，<br>整个表达式才成立。 如果检测到 条件1 的退出状态为 0，<br>就不会再检测 条件2 了，因为不管 条件2 的退出状态是什么，<br>整个表达式必然都是不成立的，检测了也是多此一举。</td></tr><tr><td>|| 或 -o</td><td>条件1 || 条件2</td><td>逻辑或运算符，条件1 和 条件2 两个表<br>达式中只要有一个成立，整个表达式就成立。 如果检<br>测到 条件1 的退出状态为 1，就不会再检测 条件2 了，因为<br>不管 条件2 的退出状态是什么，整个表达式必然都是成立的，<br>检测了也是多此一举。</td></tr><tr><td>!</td><td>!条件</td><td>逻辑非运算符，相当于“取反”的效果。如果 条件 成立，那么整<br>个表达式就不成立；如果 条件 不成立，那么整个表达式就成立。</td></tr></tbody></table><h3 id="实战演练⭐⭐" tabindex="-1">实战演练⭐⭐ <a class="header-anchor" href="#实战演练⭐⭐" aria-label="Permalink to &quot;实战演练⭐⭐&quot;">​</a></h3><p>提示输入&quot;请输入文件全名: &quot;和&quot;请输入数据:&quot; 并接收文件名与数据</p><p>使用逻辑运算符判断满足2 条件 : 文件需要具有可写权限 和 输入的数据长度不为0</p><p>满足以上2个条件 将用户输入的 数据 写入到指定的文件中去</p><p>control2.sh脚本文件代码</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入文件全名: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">filename</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入数据:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">data</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -w文件是否可写，-a表示and并且，-n数据不为空,-e表示文件是否存在</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-w</span><span style="color:#A6ACCD;"> $filename </span><span style="color:#89DDFF;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-n</span><span style="color:#A6ACCD;"> $data </span><span style="color:#89DDFF;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-e</span><span style="color:#A6ACCD;"> $filename </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $data</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $data </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> $filename</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">写入成功</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#FFCB6B;">touch</span><span style="color:#A6ACCD;"> $filename</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $data </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> $filename</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">创建并写入成功</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><blockquote><p>test命令用于对文件或字符串进行检测, <code>-w</code> 判断文件是否存在并且可写, <code>-n</code> 用于检测字符串是否非空</p><p><code>$data &gt; $filename</code> 其中 <code>&gt;</code> 用于将内容输出到指定文件中去</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241836748.png" alt="image-20220924183622614" style="zoom:80%;"><h3 id="文件测试⭐" tabindex="-1">文件测试⭐ <a class="header-anchor" href="#文件测试⭐" aria-label="Permalink to &quot;文件测试⭐&quot;">​</a></h3><table><thead><tr><th style="text-align:left;">参数</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">==-e 文件名==</td><td style="text-align:left;">exists, 如果文件存在，则为真</td></tr><tr><td style="text-align:left;">==-r 文件名==</td><td style="text-align:left;">read,如果文件存在且可读，则为真</td></tr><tr><td style="text-align:left;">==-w 文件名==</td><td style="text-align:left;">write,如果文件存在且可写，则为真</td></tr><tr><td style="text-align:left;">==-x 文件名==</td><td style="text-align:left;">execute,如果文件存在且可执行，则为真</td></tr><tr><td style="text-align:left;">==-s 文件名==</td><td style="text-align:left;">string,如果文件存在且至少有一个字符，则为真</td></tr><tr><td style="text-align:left;">==-d 文件名==</td><td style="text-align:left;">directory,如果文件存在且为目录，则为真</td></tr><tr><td style="text-align:left;">-f 文件名</td><td style="text-align:left;">file,如果文件存在且为普通文件，则为真</td></tr><tr><td style="text-align:left;">-c 文件名</td><td style="text-align:left;">character,如果文件存在且为字符型特殊文件，则为真</td></tr><tr><td style="text-align:left;">-b 文件名</td><td style="text-align:left;">如果文件存在且为块特殊文件，则为真</td></tr></tbody></table><p>control5.sh脚本代码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-w</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">control1.sh</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">文件已存在并且可写!</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">文件不存在或不可写!</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-e</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">control1.sh</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-e</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">control2.sh</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">两个文件都存在!</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">可能有一个或两个文件不存在</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><p>Shell提供了与( -a )、或( -o )、非( ! )三个逻辑操作符用于将测试条件连接起来，其优先级为：&quot;!&quot;最高，&quot;-a&quot;次之，&quot;-o&quot;最低, 语法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">test</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">条件1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-o</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">条件2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">条件3</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">...</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120790.png" alt="image-20200704074322642"></p><h2 id="case语句" tabindex="-1">case语句 <a class="header-anchor" href="#case语句" aria-label="Permalink to &quot;case语句&quot;">​</a></h2><p>Shell case语句为多选择语句。可以用case语句匹配一个值与一个模式，如果匹配成功，执行相匹配的命令;</p><p>当分支较多，并且判断条件比较简单时，使用 case in 语句就比较方便了。</p><h3 id="语法规则-7" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-7" aria-label="Permalink to &quot;语法规则&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">case</span><span style="color:#A6ACCD;"> 值 </span><span style="color:#89DDFF;font-style:italic;">in</span></span>
<span class="line"><span style="color:#A6ACCD;">匹配模式1</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令1</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令2</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">...</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">匹配模式2）</span></span>
<span class="line"><span style="color:#A6ACCD;">    命令1</span></span>
<span class="line"><span style="color:#A6ACCD;">    命令2</span></span>
<span class="line"><span style="color:#A6ACCD;">    ...</span></span>
<span class="line"><span style="color:#A6ACCD;">    ;;</span></span>
<span class="line"><span style="color:#89DDFF;">*)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令1</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令2</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">...</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">esac</span></span></code></pre></div><p>每一匹配模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后，其间所有命令开始执行直至 ;;(类似break, 不可以替代否则语法报错)。取值将检测匹配的每一个模式。一旦模式匹配，则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式，使用星号 * 捕获该值，再执行后面的命令。</p><p>case、in 和 esac 都是 Shell 关键字, esac就是case的反写在这里代表结束case</p><p>匹配模式: 可以是一个数字、一个字符串，甚至是一个简单正则表达式。</p><p>简单正则表达式支持如下通配符</p><table><thead><tr><th>格式</th><th>说明</th></tr></thead><tbody><tr><td>*</td><td>表示任意字符串。</td></tr><tr><td>[abc]</td><td>表示 a、b、c 三个字符中的任意一个。比如，[15ZH] 表示 1、5、Z、H 四个字符中的任意一个。</td></tr><tr><td>[m-n]</td><td>表示从 m 到 n 的任意一个字符。比如，[0-9] 表示任意一个数字，[0-9a-zA-Z] 表示字母或数字。</td></tr><tr><td>|</td><td>表示多重选择，类似逻辑运算中的或运算。比如，abc | xyz 表示匹配字符串 &quot;abc&quot; 或者 &quot;xyz&quot;。</td></tr></tbody></table><h3 id="案例演示-8" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-8" aria-label="Permalink to &quot;案例演示&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入一个0~7的数字:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">number</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">case</span><span style="color:#A6ACCD;"> $number </span><span style="color:#89DDFF;font-style:italic;">in</span></span>
<span class="line"><span style="color:#A6ACCD;">1</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">星期一</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">2</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">星期二</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">3</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">星期三</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">4</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">星期四</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">5</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">星期五</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">6</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">星期六</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">0</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;">7</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">星期日</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#89DDFF;">*)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">您输入的数字无效</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">;;</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">esac</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120794.png" alt="image-20200704142506942"></p><h2 id="while语句" tabindex="-1">while语句 <a class="header-anchor" href="#while语句" aria-label="Permalink to &quot;while语句&quot;">​</a></h2><blockquote><p>while用于循环执行一系列命令</p></blockquote><h3 id="语法规则-8" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-8" aria-label="Permalink to &quot;语法规则&quot;">​</a></h3><p>多行写法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">条件</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#FFCB6B;">命令1</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#FFCB6B;">命令2</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#82AAFF;">...</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#FFCB6B;">continue</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;"># 结束当前这一次循环, 进入下一次循环</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#89DDFF;font-style:italic;">break</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;"># 结束当前循环</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><p>一行写法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">条件</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">命令</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span><span style="color:#89DDFF;">;</span></span></code></pre></div><h3 id="案例演示-9" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-9" aria-label="Permalink to &quot;案例演示&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入一个数字:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">number</span></span>
<span class="line"><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;"> $i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> $number </span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello world</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">++))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209241948529.png" alt="image-20220924194810389" style="zoom:80%;"><h3 id="无限循环" tabindex="-1">无限循环 <a class="header-anchor" href="#无限循环" aria-label="Permalink to &quot;无限循环&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">:</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">command</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">true</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">command</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">true</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">123</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><h3 id="break-continue" tabindex="-1">break &amp; continue <a class="header-anchor" href="#break-continue" aria-label="Permalink to &quot;break &amp; continue&quot;">​</a></h3><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入一个数字:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">number</span></span>
<span class="line"><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;"> $i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> $number </span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello world</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">let</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">i++</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">==</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">退出</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;font-style:italic;">break</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121502434.png" alt="image-20230312150225203" style="zoom:80%;"><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入一个数字:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">number</span></span>
<span class="line"><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;"> $i </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> $number </span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello world</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">let</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">i++</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">==</span><span style="color:#F78C6C;">3</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">退出</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#FFCB6B;">continue</span><span style="color:#89DDFF;">;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121504443.png" alt="image-20230312150431284" style="zoom:80%;"><h2 id="until语句" tabindex="-1">until语句 <a class="header-anchor" href="#until语句" aria-label="Permalink to &quot;until语句&quot;">​</a></h2><blockquote><p>until 也是循环结构语句, until 循环与 while 循环在处理方式上刚好相反, 循环条件为false会一致循环, 条件为true停止循环.</p></blockquote><h3 id="语法规则-9" tabindex="-1">语法规则 <a class="header-anchor" href="#语法规则-9" aria-label="Permalink to &quot;语法规则&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">until</span><span style="color:#A6ACCD;"> 条件</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><p>条件如果返回值为1(代表false)，则继续执行循环体内的语句，否则跳出循环。</p><h3 id="案例演示-10" tabindex="-1">案例演示 <a class="header-anchor" href="#案例演示-10" aria-label="Permalink to &quot;案例演示&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入一个数字:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">number</span></span>
<span class="line"><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">until</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">$i </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> $number</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello world</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">++))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242125269.png" alt="image-20220924212533069" style="zoom:80%;"><h2 id="for循环" tabindex="-1">for循环 <a class="header-anchor" href="#for循环" aria-label="Permalink to &quot;for循环&quot;">​</a></h2><h3 id="循环方式1" tabindex="-1">循环方式1 <a class="header-anchor" href="#循环方式1" aria-label="Permalink to &quot;循环方式1&quot;">​</a></h3><p>多行写法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> var </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> item1 item2 ... itemN</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令1</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令2</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">...</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><p>一行写法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> var </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> item1 item2 ... itemN</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">命令1</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">命令2…</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span><span style="color:#89DDFF;">;</span></span></code></pre></div><blockquote><p>var是循环变量，item1 item2 ... itemN 是循环的范围</p></blockquote><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入一个循环的数字:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">number</span></span>
<span class="line"><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> 1 3 5 6</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242145832.png" alt="image-20220924214536680" style="zoom:80%;"><h3 id="循环方式2" tabindex="-1">循环方式2 <a class="header-anchor" href="#循环方式2" aria-label="Permalink to &quot;循环方式2&quot;">​</a></h3><p>多行写法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> var </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#FFCB6B;">start..end}</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#FFCB6B;">命令</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><blockquote><p>start: 循环范围的起始值,必须为整数</p><p>end: 循环范围的结束值, 必须为整数</p></blockquote><p>一行写法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> var </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#FFCB6B;">start..end}</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">命令</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><p>循环1到5并打印</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#FFCB6B;">1..5}</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $i</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> i </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#FFCB6B;">1..10}</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">}&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242147881.png" alt="image-20220924214735707" style="zoom:80%;"><h3 id="循环方式3" tabindex="-1">循环方式3 <a class="header-anchor" href="#循环方式3" aria-label="Permalink to &quot;循环方式3&quot;">​</a></h3><p>多行写法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">for</span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">start;i</span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;">end;i</span><span style="color:#89DDFF;">++))</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#FFCB6B;">命令</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><p>一行写法</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">for</span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">start;i</span><span style="color:#89DDFF;">&lt;=</span><span style="color:#A6ACCD;">end;i</span><span style="color:#89DDFF;">++));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">命令</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">for</span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">;i</span><span style="color:#89DDFF;">&lt;=</span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;">;i</span><span style="color:#89DDFF;">++));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">hello,</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">}&quot;</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242149107.png" alt="image-20220924214954932" style="zoom:80%;"><h3 id="无限循环-1" tabindex="-1">无限循环 <a class="header-anchor" href="#无限循环-1" aria-label="Permalink to &quot;无限循环&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">for</span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">;;</span><span style="color:#89DDFF;">));</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">命令</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><h2 id="select语句" tabindex="-1">select语句 <a class="header-anchor" href="#select语句" aria-label="Permalink to &quot;select语句&quot;">​</a></h2><blockquote><p>select in 循环用来增强交互性，它可以显示出带编号的菜单，用户输入不同的编号就可以选择不同的菜单，并执行不同的功能. select in 是 Shell 独有的一种循环，非常适合终端（Terminal）这样的交互场景, 其他语言没有;常与 case in 一起配合使用</p></blockquote><h3 id="语法" tabindex="-1">语法 <a class="header-anchor" href="#语法" aria-label="Permalink to &quot;语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">select</span><span style="color:#A6ACCD;"> var </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> menu1 menu2 ...</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><blockquote><p>注意: select 是无限循环（死循环），输入空值，或者输入的值无效，都不会结束循环，只有遇到 break 语句，或者按下 Ctrl+D 组合键才能结束循环。</p><p>执行命令过程中: 终端会输出 <code>#?</code> 代表可以输入选择的菜单编号</p></blockquote><h3 id="演示1" tabindex="-1">演示1 <a class="header-anchor" href="#演示1" aria-label="Permalink to &quot;演示1&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">你的爱好是什么?</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">select</span><span style="color:#A6ACCD;"> hobby </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">编程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">游戏</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">篮球</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">游泳</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $hobby</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">break</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">你的爱好是:</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">hobby</span><span style="color:#89DDFF;">}&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242152075.png" alt="image-20220924215249922" style="zoom:80%;"><h3 id="演示2" tabindex="-1">演示2 <a class="header-anchor" href="#演示2" aria-label="Permalink to &quot;演示2&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">你的爱好是什么</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">select</span><span style="color:#A6ACCD;"> hobby </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">编程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">游戏</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">篮球</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">游泳</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">case</span><span style="color:#A6ACCD;"> $hobby </span><span style="color:#89DDFF;font-style:italic;">in</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">编程</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">编程,多敲代码</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;font-style:italic;">break</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">游戏</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">少玩游戏</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;font-style:italic;">break</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">篮球</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">|</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">游泳</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">运动有利健康</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;font-style:italic;">break</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#89DDFF;">;;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">*)</span></span>
<span class="line"><span style="color:#A6ACCD;">            </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">输入错误，请重新输入</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">esac</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242153028.png" alt="image-20220924215341871" style="zoom:80%;"><h1 id="系统函数" tabindex="-1">系统函数 <a class="header-anchor" href="#系统函数" aria-label="Permalink to &quot;系统函数&quot;">​</a></h1><h2 id="函数介绍" tabindex="-1">函数介绍 <a class="header-anchor" href="#函数介绍" aria-label="Permalink to &quot;函数介绍&quot;">​</a></h2><p>Shell编程和其他编程语言一样, 有函数, 函数是由若干条shell命令组成的语句块，实现Shell脚本代码重用和模块化编程</p><p>函数分类：系统函数、自定义函数</p><h2 id="系统函数-1" tabindex="-1">系统函数 <a class="header-anchor" href="#系统函数-1" aria-label="Permalink to &quot;系统函数&quot;">​</a></h2><h3 id="basename" tabindex="-1">basename <a class="header-anchor" href="#basename" aria-label="Permalink to &quot;basename&quot;">​</a></h3><blockquote><p>basename函数用于获取文件名的函数, 根据给出的文件路径截取出文件名</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">basename </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">string </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;"> pathname</span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">suffix</span><span style="color:#89DDFF;">]</span></span></code></pre></div><blockquote><p>根据根据指定字符串或路径名进行截取文件名, 比如: 根据路径&quot;/one/two/aa.txt&quot;, 可以截取出aa.txt</p><p>suffix: 用于截取的时候去掉指定的后缀名.</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">basename </span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">root</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">itheima</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">t</span><span style="color:#89DDFF;">.</span><span style="color:#A6ACCD;">txt</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120326.png" alt="image-20200704175505682"></p><h3 id="dirname系统函数" tabindex="-1">dirname系统函数 <a class="header-anchor" href="#dirname系统函数" aria-label="Permalink to &quot;dirname系统函数&quot;">​</a></h3><blockquote><p>从指定的文件绝对路径, 去除文件名，返回剩下的前缀目录路径</p></blockquote><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">dirname 文件绝对路径</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120330.png" alt="image-20200704184207793"></p><h2 id="自定义函数⭐" tabindex="-1">自定义函数⭐ <a class="header-anchor" href="#自定义函数⭐" aria-label="Permalink to &quot;自定义函数⭐&quot;">​</a></h2><blockquote><p>开发人员可以通过自定义开发函数,实现代码重用.</p></blockquote><h3 id="基本语法-4" tabindex="-1">基本语法 <a class="header-anchor" href="#基本语法-4" aria-label="Permalink to &quot;基本语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 函数的定义</span></span>
<span class="line"><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> function </span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">funname</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">命令</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">return 返回值</span><span style="color:#89DDFF;">]</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 调用函数</span></span>
<span class="line"><span style="color:#FFCB6B;">funname</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">传递参数1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">传递参数2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">...</span></span></code></pre></div><blockquote><ol><li><p>可以带function fun() 定义，也可以直接fun() 定义,不带任何参数。</p></li><li><p>参数返回，可以显示加：return 返回，如果不加，将以最后一条命令运行结果，作为返回值。 return后跟数值n(0~255)</p></li></ol></blockquote><blockquote><p>必须在调用函数地方之前，先声明函数，shell脚本是逐行运行, 只要先运行了函数, 后面才可以时使用函数</p></blockquote><h3 id="示例-无参无返回值函数" tabindex="-1">示例：无参无返回值函数 <a class="header-anchor" href="#示例-无参无返回值函数" aria-label="Permalink to &quot;示例：无参无返回值函数&quot;">​</a></h3><p>文件脚本代码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">demo</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">执行了函数</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 调用函数</span></span>
<span class="line"><span style="color:#FFCB6B;">demo</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242203609.png" alt="image-20220924220350451" style="zoom:80%;"><h3 id="示例-无参有返回值函数" tabindex="-1">示例：无参有返回值函数 <a class="header-anchor" href="#示例-无参有返回值函数" aria-label="Permalink to &quot;示例：无参有返回值函数&quot;">​</a></h3><p>fun2.sh文件脚本代码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">sum</span><span style="color:#89DDFF;">()</span></span>
<span class="line"><span style="color:#89DDFF;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">求两个数的和...</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">输入第一个数字: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">n1</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">输入第二个数字: </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">n2</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">两个数字分别为 </span><span style="color:#A6ACCD;">$n1</span><span style="color:#C3E88D;"> 和 </span><span style="color:#A6ACCD;">$n2</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">return</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">$((</span><span style="color:#A6ACCD;">$n1</span><span style="color:#89DDFF;">+</span><span style="color:#A6ACCD;">$n2</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 调用函数</span></span>
<span class="line"><span style="color:#FFCB6B;">sum</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">两个数字的和为: </span><span style="color:#A6ACCD;">$?</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;"># 获取函数返回值</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242204199.png" alt="image-20220924220436040" style="zoom:80%;"><h3 id="示例-有参函数" tabindex="-1">示例：有参函数 <a class="header-anchor" href="#示例-有参函数" aria-label="Permalink to &quot;示例：有参函数&quot;">​</a></h3><h4 id="语法介绍-2" tabindex="-1">语法介绍 <a class="header-anchor" href="#语法介绍-2" aria-label="Permalink to &quot;语法介绍&quot;">​</a></h4><p>在Shell中，调用函数时可以向其传递参数。在函数体内部，通过 <code>$n</code> 的形式来获取参数的值，例如，<code>$1</code> 表示第一个参数，<code>$2</code> 表示第二个参数...</p><p>其他参数介绍</p><table><thead><tr><th style="text-align:left;">参数处理</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">$#</td><td style="text-align:left;">传递到脚本或函数的参数个数</td></tr><tr><td style="text-align:left;">$*</td><td style="text-align:left;">以一个单字符串显示所有向脚本传递的参数</td></tr><tr><td style="text-align:left;">$$</td><td style="text-align:left;">脚本运行的当前进程ID号</td></tr><tr><td style="text-align:left;">$!</td><td style="text-align:left;">后台运行的最后一个进程的ID号</td></tr><tr><td style="text-align:left;">$@</td><td style="text-align:left;">与$*相同，但是使用时加引号，并在引号中返回每个参数。</td></tr><tr><td style="text-align:left;">$?</td><td style="text-align:left;">显示最后命令的退出状态。0表示没有错误，其他任何值表明有错误。</td></tr></tbody></table><h4 id="示例演示" tabindex="-1">示例演示 <a class="header-anchor" href="#示例演示" aria-label="Permalink to &quot;示例演示&quot;">​</a></h4><p>fun3.sh文件脚本代码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">funParam</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第一个参数为 </span><span style="color:#A6ACCD;font-style:italic;">$1</span><span style="color:#C3E88D;"> !</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第二个参数为 </span><span style="color:#A6ACCD;font-style:italic;">$2</span><span style="color:#C3E88D;"> !</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第十个参数为 </span><span style="color:#A6ACCD;">$10</span><span style="color:#C3E88D;"> !</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第十个参数为 </span><span style="color:#A6ACCD;font-style:italic;">${10}</span><span style="color:#C3E88D;"> !</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">第十一个参数为 </span><span style="color:#A6ACCD;font-style:italic;">${11}</span><span style="color:#C3E88D;"> !</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">参数总数有 </span><span style="color:#A6ACCD;">$#</span><span style="color:#C3E88D;"> 个!</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">作为一个字符串输出所有参数 </span><span style="color:#A6ACCD;">$*</span><span style="color:#C3E88D;"> !</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 调用函数</span></span>
<span class="line"><span style="color:#FFCB6B;">funParam</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">3</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">4</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">5</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">6</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">7</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">8</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">9</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">22</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242205529.png" alt="image-20220924220555364" style="zoom:80%;"><h2 id="shell程序与函数的区别" tabindex="-1">shell程序与函数的区别 <a class="header-anchor" href="#shell程序与函数的区别" aria-label="Permalink to &quot;shell程序与函数的区别&quot;">​</a></h2><p>函数和shell程序比较相似，区别在于：</p><ul><li>Shell 程序(内置命令和外部脚本文件), 外部脚本文件是在子Shell中运行, 会开启独立的进程运行</li><li>Shell函数在当前Shell的进程中运行</li></ul><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">demo</span><span style="color:#89DDFF;">(){</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">函数中打印当前进程ID:</span><span style="color:#A6ACCD;">$$</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">当前脚本文件(Shell程序)打印当前进程ID:</span><span style="color:#A6ACCD;">$$</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 调用函数</span></span>
<span class="line"><span style="color:#FFCB6B;">demo</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242208232.png" alt="image-20220924220853075" style="zoom:80%;"><h1 id="重定向输入输出⭐" tabindex="-1">重定向输入输出⭐ <a class="header-anchor" href="#重定向输入输出⭐" aria-label="Permalink to &quot;重定向输入输出⭐&quot;">​</a></h1><h2 id="重定向介绍" tabindex="-1">重定向介绍 <a class="header-anchor" href="#重定向介绍" aria-label="Permalink to &quot;重定向介绍&quot;">​</a></h2><h3 id="标准输入介绍" tabindex="-1">标准输入介绍 <a class="header-anchor" href="#标准输入介绍" aria-label="Permalink to &quot;标准输入介绍&quot;">​</a></h3><p>从键盘读取用户输入的数据，然后再把数据拿到Shell程序中使用；</p><h3 id="标准输出介绍" tabindex="-1">标准输出介绍 <a class="header-anchor" href="#标准输出介绍" aria-label="Permalink to &quot;标准输出介绍&quot;">​</a></h3><p>Shell程序产生的数据，这些数据一般都是呈现到显示器上供用户浏览查看;</p><h3 id="默认输入输出文件" tabindex="-1">默认输入输出文件 <a class="header-anchor" href="#默认输入输出文件" aria-label="Permalink to &quot;默认输入输出文件&quot;">​</a></h3><p>每个 Unix/Linux 命令运行时都会打开三个文件, 文件如下</p><table><thead><tr><th>文件名</th><th>类型</th><th>文件描述符(file description, fd)</th><th>功能</th></tr></thead><tbody><tr><td>stdin</td><td>(standard input)<br>标准输入文件</td><td>0</td><td>获取键盘的输入数据</td></tr><tr><td>stdout</td><td>(standard output)<br>标准输出文件</td><td>1</td><td>将正确数据输出到显示器上</td></tr><tr><td>stderr</td><td>(standard error)<br>标准错误输出文件</td><td>2</td><td>将错误信息输出到显示器上</td></tr></tbody></table><blockquote><p>每个文件都有一个唯一的 <strong>文件描述符fd</strong>, 后面会通过唯一 <strong>文件描述符fd</strong> 操作对应的信息</p></blockquote><p>Shell程序操作输入输出时用到这3个文件</p><ol><li>Shell程序默认会从stdin文件中读取输入数据</li><li>Shell程序默认会向stdout文件输出正确数据</li><li>Shell程序默认会项stderr文件中输出错误信息</li></ol><p>这3个文件用于临时传输数据使用</p><h3 id="重定向输入输出介绍" tabindex="-1">重定向输入输出介绍 <a class="header-anchor" href="#重定向输入输出介绍" aria-label="Permalink to &quot;重定向输入输出介绍&quot;">​</a></h3><p>标准输入是数据默认从键盘流向程序，如果改变了它的方向，数据就从其它地方流入，这就是输入重定向。</p><p>标准输出是数据默认从程序流向显示器，如果改变了它的方向，数据就流向其它地方，这就是输出重定向。</p><blockquote><p>Linux Shell 重定向分为两种，一种输入重定向，一种是输出重定向；</p></blockquote><h3 id="重定向的作用" tabindex="-1">重定向的作用 <a class="header-anchor" href="#重定向的作用" aria-label="Permalink to &quot;重定向的作用&quot;">​</a></h3><p>输出重定向是指命令的结果不再输出到显示器上，而是输出到其它地方，一般是文件中。这样做的最大好处就是把命令的结果保存起来，当我们需要的时候可以随时查询。</p><h2 id="重定向语法⭐" tabindex="-1">重定向语法⭐ <a class="header-anchor" href="#重定向语法⭐" aria-label="Permalink to &quot;重定向语法⭐&quot;">​</a></h2><table><thead><tr><th style="text-align:left;">命令</th><th style="text-align:left;">说明</th></tr></thead><tbody><tr><td style="text-align:left;">命令 &gt; file</td><td style="text-align:left;">将正确数据重定向输出到 file 文件中, 覆盖方式</td></tr><tr><td style="text-align:left;">命令 &lt; file</td><td style="text-align:left;">将输入重定向从 file 文件中读取数据</td></tr><tr><td style="text-align:left;">命令 &gt;&gt; file</td><td style="text-align:left;">将正确数据重定向输出到 file 文件中, 追加方式</td></tr><tr><td style="text-align:left;">命令 &lt; file1 &gt; file2</td><td style="text-align:left;">从file文件读取数据, 输出数据到file2文件中</td></tr><tr><td style="text-align:left;">命令 fd&gt; file</td><td style="text-align:left;">根据指定的文件描述符fd 将数据重定向输出到 file 文件中, 覆盖方式</td></tr><tr><td style="text-align:left;">命令 fd&gt;&gt; file</td><td style="text-align:left;">根据指定的文件描述符fd 将数据重定向输出到 file 文件中, 追加方式</td></tr><tr><td style="text-align:left;">命令 &gt; file fd1&gt;&amp; fd2</td><td style="text-align:left;">将 fd1 和 fd2 文件描述符合并 输出到文件。</td></tr><tr><td style="text-align:left;">fd1&lt;&amp; fd2</td><td style="text-align:left;">将 fd1 和 fd2 文件描述符合并 从文件读取输入.</td></tr><tr><td style="text-align:left;">&lt;&lt; tag</td><td style="text-align:left;">读取终端输入数据, 将开始标记 tag 和结束标记 tag 之间的内容作为输入。<br>标记名tag可以任意</td></tr></tbody></table><blockquote><p>在输出重定向中，<code>&gt;</code>代表的是覆盖输出，<code>&gt;&gt;</code>代表的是追加输出。</p><p>fd是文件描述符</p><p>​ 0 通常是标准输入（STDIN），</p><p>​ 1 是标准输出（STDOUT），</p><p>​ 2 是标准错误输出（STDERR）。</p><p>fd&gt; 或 fd&gt;&gt; 中间不可以有空格</p></blockquote><h2 id="文件写入覆盖" tabindex="-1">文件写入覆盖 <a class="header-anchor" href="#文件写入覆盖" aria-label="Permalink to &quot;文件写入覆盖&quot;">​</a></h2><h3 id="_1、覆盖" tabindex="-1">1、覆盖 <a class="header-anchor" href="#_1、覆盖" aria-label="Permalink to &quot;1、覆盖&quot;">​</a></h3><p>这里有两种格式可以使用</p><p>格式一</p><div class="language-bash"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#FFCB6B;">cat</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&lt;&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">EOF</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> /root/test.txt</span></span>
<span class="line"><span style="color:#C3E88D;">Hello!</span></span>
<span class="line"><span style="color:#C3E88D;">My site is www.361way.com</span></span>
<span class="line"><span style="color:#C3E88D;">My site is www.91it.org</span></span>
<span class="line"><span style="color:#C3E88D;">Test for cat and EOF!</span></span>
<span class="line"><span style="color:#89DDFF;">EOF</span></span></code></pre></div><p>格式二</p><div class="language-bash"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#FFCB6B;">cat</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/test.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&lt;&lt;</span><span style="color:#89DDFF;">EOF</span></span>
<span class="line"><span style="color:#C3E88D;">Hello!</span></span>
<span class="line"><span style="color:#C3E88D;">My site is www.361way.com</span></span>
<span class="line"><span style="color:#C3E88D;">My site is www.91it.org</span></span>
<span class="line"><span style="color:#C3E88D;">Test for cat and EOF!</span></span>
<span class="line"><span style="color:#89DDFF;">EOF</span></span></code></pre></div><p>两种写法区别无法是要写入的文件放在中间或最后的问题，至于选哪种看个人喜好吧。</p><h3 id="_2、追加" tabindex="-1">2、追加 <a class="header-anchor" href="#_2、追加" aria-label="Permalink to &quot;2、追加&quot;">​</a></h3><p>覆盖的写法基本和追加一样，不同的是单重定向号变成双重定向号。</p><p>格式一</p><div class="language-bash"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#FFCB6B;">cat</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&lt;&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">EOF</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#A6ACCD;"> /root/test.txt</span></span>
<span class="line"><span style="color:#C3E88D;">Hello!</span></span>
<span class="line"><span style="color:#C3E88D;">My site is www.361way.com</span></span>
<span class="line"><span style="color:#C3E88D;">My site is www.91it.org</span></span>
<span class="line"><span style="color:#C3E88D;">Test for cat and EOF!</span></span>
<span class="line"><span style="color:#89DDFF;">EOF</span></span></code></pre></div><p>格式二</p><div class="language-bash"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#FFCB6B;">cat</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/test.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&lt;&lt;</span><span style="color:#89DDFF;">EOF</span></span>
<span class="line"><span style="color:#C3E88D;">Hello!</span></span>
<span class="line"><span style="color:#C3E88D;">My site is www.361way.com</span></span>
<span class="line"><span style="color:#C3E88D;">My site is www.91it.org</span></span>
<span class="line"><span style="color:#C3E88D;">Test for cat and EOF!</span></span>
<span class="line"><span style="color:#89DDFF;">EOF</span></span></code></pre></div><p>需要注意的是，不论是覆盖还是追加，在涉及到变量操作时是需要进行转义的，例如：</p><div class="language-bash"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#FFCB6B;">cat</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&lt;&lt;</span><span style="color:#89DDFF;">EOF</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#A6ACCD;"> /root/a.txt</span></span>
<span class="line"><span style="color:#C3E88D;">PATH=</span><span style="color:#A6ACCD;">\$</span><span style="color:#C3E88D;">PATH:</span><span style="color:#A6ACCD;">\$</span><span style="color:#C3E88D;">HOME/bin</span></span>
<span class="line"><span style="color:#C3E88D;">export ORACLE_BASE=/u01/app/oracle</span></span>
<span class="line"><span style="color:#C3E88D;">export ORACLE_HOME=</span><span style="color:#A6ACCD;">\$</span><span style="color:#C3E88D;">ORACLE_BASE/10.2.0/db_1</span></span>
<span class="line"><span style="color:#C3E88D;">export ORACLE_SID=yqpt</span></span>
<span class="line"><span style="color:#C3E88D;">export PATH=</span><span style="color:#A6ACCD;">\$</span><span style="color:#C3E88D;">PATH:</span><span style="color:#A6ACCD;">\$</span><span style="color:#C3E88D;">ORACLE_HOME/bin</span></span>
<span class="line"><span style="color:#C3E88D;">export NLS_LANG=&quot;AMERICAN_AMERICA.AL32UTF8&quot;</span></span>
<span class="line"><span style="color:#89DDFF;">EOF</span></span></code></pre></div><h3 id="_3、语法" tabindex="-1">3、语法 <a class="header-anchor" href="#_3、语法" aria-label="Permalink to &quot;3、语法&quot;">​</a></h3><blockquote><p>tee [-ai][--help][--version][文件...]</p></blockquote><p>参数：</p><div class="language-bash"><button title="Copy Code" class="copy"></button><span class="lang">bash</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">-a或--append</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">　附加到既有文件的后面，而非覆盖它．</span></span>
<span class="line"><span style="color:#FFCB6B;">-i或--ignore-interrupts</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">　忽略中断信号。</span></span>
<span class="line"><span style="color:#FFCB6B;">--help</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">　在线帮助。</span></span>
<span class="line"><span style="color:#FFCB6B;">--version</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">　显示版本信息。</span></span></code></pre></div><p>实例 使用指令&quot;tee&quot;将用户输入的数据同时保存到文件&quot;file1&quot;和&quot;file2&quot;中，输入如下命令：</p><p>$ tee file1 file2 #在两个文件中复制内容</p><p>以上命令执行后，将提示用户输入需要保存到文件的数据，如下所示：</p><p>My Linux #提示用户输入数据 My Linux #输出数据，进行输出反馈</p><p>此时，可以分别打开文件&quot;file1&quot;和&quot;file2&quot;，查看其内容是否均是&quot;My Linux&quot;即可判断指令&quot;tee&quot;是否执行成功。</p><h2 id="输出示例-正确信息重定向输出" tabindex="-1">输出示例：正确信息重定向输出 <a class="header-anchor" href="#输出示例-正确信息重定向输出" aria-label="Permalink to &quot;输出示例：正确信息重定向输出&quot;">​</a></h2><p>创建文件redirect1.txt</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">touch</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">redirect1.txt</span></span></code></pre></div><p>执行who命令重定向输出到redirect1.txt文件中</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itheima</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">redirect1.txt</span></span>
<span class="line"><span style="color:#FFCB6B;">ls</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">redirect2.txt</span></span>
<span class="line"><span style="color:#82AAFF;">pwd</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">redirect2.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242215974.png" alt="image-20220924221533793" style="zoom:80%;"><h2 id="输出示例-错误信息重定向输出" tabindex="-1">输出示例：错误信息重定向输出 <a class="header-anchor" href="#输出示例-错误信息重定向输出" aria-label="Permalink to &quot;输出示例：错误信息重定向输出&quot;">​</a></h2><p>预览错误消息</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ls</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">java</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120844.png" alt="image-20200705214312724"></p><p>没有java目录所以报错，将错误消息输出到redirect2.txt文件中，该文件不用手动创建</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ls</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">java</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">2&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">redirect2.txt</span></span></code></pre></div><blockquote><p>2 是标准错误输出（STDERR）, 注意</p><p><code>&gt;</code> 覆盖方式输出</p><p><code>2&gt;</code> 注意fd与&gt;符号之间不能有空格</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242218254.png" alt="image-20220924221843081" style="zoom:80%;"><h2 id="输出示例-正确和错误信息同时输出⭐" tabindex="-1">输出示例：正确和错误信息同时输出⭐ <a class="header-anchor" href="#输出示例-正确和错误信息同时输出⭐" aria-label="Permalink to &quot;输出示例：正确和错误信息同时输出⭐&quot;">​</a></h2><p>将正确信息与错误信息都保存到一个文件中</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">itcast</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">redirect2.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">2&gt;&amp;1</span></span></code></pre></div><blockquote><p>数字 1 代表正确输出的结果输出到文件中 数字 2 代表错误结果输出到文件中</p><p><code>2&gt;&amp; 1</code> 将正确和错误都输出到文件中. <code>2&gt;&amp;</code> 中间不能有空格, 写法有2种</p><p>合并第一种写法: <code>2&gt;&amp; 1</code>、合并第二种写法: <code>2&gt;&amp;1</code></p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242222471.png" alt="image-20220924222229287" style="zoom:80%;"><h2 id="输入示例-统计文件数据行数" tabindex="-1">输入示例：统计文件数据行数 <a class="header-anchor" href="#输入示例-统计文件数据行数" aria-label="Permalink to &quot;输入示例：统计文件数据行数&quot;">​</a></h2><h3 id="wc命令介绍" tabindex="-1">wc命令介绍 <a class="header-anchor" href="#wc命令介绍" aria-label="Permalink to &quot;wc命令介绍&quot;">​</a></h3><blockquote><p>Linux wc 命令可以用来对文本进行统计，包括单词个数、行数、字节数</p></blockquote><h3 id="wc命令语法" tabindex="-1">wc命令语法 <a class="header-anchor" href="#wc命令语法" aria-label="Permalink to &quot;wc命令语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">wc</span><span style="color:#A6ACCD;">  [options]  </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">文件名</span><span style="color:#89DDFF;">]</span></span></code></pre></div><p>options有如下:</p><table><thead><tr><th>选项</th><th>含义</th></tr></thead><tbody><tr><td><code>-c</code></td><td>character, 统计字节数</td></tr><tr><td><code>-w</code></td><td>word, 统计单词数</td></tr><tr><td><code>-l</code></td><td>line, 统计行数</td></tr></tbody></table><h3 id="演示" tabindex="-1">演示 <a class="header-anchor" href="#演示" aria-label="Permalink to &quot;演示&quot;">​</a></h3><p>统计文件redirect2.txt中数据行数</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">wc</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-l</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">redirect2.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242223686.png" style="zoom:80%;"><h2 id="输入示例-逐行读取文件数据" tabindex="-1">输入示例：逐行读取文件数据 <a class="header-anchor" href="#输入示例-逐行读取文件数据" aria-label="Permalink to &quot;输入示例：逐行读取文件数据&quot;">​</a></h2><p>循环读取文件每一行数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">while</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">str</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">do</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $str</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">done</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;"> redirect2.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242224353.png" alt="image-20220924222403176" style="zoom:80%;"><h2 id="输入示例-读取终端输入数据的行数" tabindex="-1">输入示例：读取终端输入数据的行数 <a class="header-anchor" href="#输入示例-读取终端输入数据的行数" aria-label="Permalink to &quot;输入示例：读取终端输入数据的行数&quot;">​</a></h2><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121540573.png" alt="image-20230312154010409" style="zoom:80%;"><h1 id="工具篇⭐⭐" tabindex="-1">工具篇⭐⭐ <a class="header-anchor" href="#工具篇⭐⭐" aria-label="Permalink to &quot;工具篇⭐⭐&quot;">​</a></h1><p>grep , sed ,awk , cut 文本字符串操作四剑客的区别</p><blockquote><p>grep：<strong>查找匹配的行</strong></p><p>cut： <strong>截取数据. 截取某个文件中的列, 重点是按照列分割, 这个命令不适合截取文件中有多个空白字符的字段</strong></p><p>sed： <strong>增删改查数据. sed用于在文件中以行来截取数据进行增\删\改\查</strong></p><p>awk：<strong>截取分析数据. 可以在某个文件中是以竖列来截取分析数据</strong>,</p></blockquote><h2 id="cut文本切割" tabindex="-1">cut文本切割 <a class="header-anchor" href="#cut文本切割" aria-label="Permalink to &quot;cut文本切割&quot;">​</a></h2><blockquote><p>一个强大文本处理工具，它可以将文本按列进行划分的文本处理。cut命令逐行读入文本，然后按列划分字段并进行提取、输出等操作。</p></blockquote><h3 id="语法分析-2" tabindex="-1">语法分析 <a class="header-anchor" href="#语法分析-2" aria-label="Permalink to &quot;语法分析&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> [options]  filename</span></span></code></pre></div><p>options参数说明</p><table><thead><tr><th>选项参数</th><th>功能</th></tr></thead><tbody><tr><td>-f 提取范围</td><td>列号，获取第几列</td></tr><tr><td>-d 自定义分隔符</td><td>自定义分隔符，默认为制表符。</td></tr><tr><td>-c 提取范围</td><td>以字符为单位进行分割</td></tr><tr><td>-b 提取范围</td><td>以字节为单位进行分割。这些字节位置将忽略多字节字符边界，除非也指定了 -n 标志。</td></tr><tr><td>-n</td><td>与“-b”选项连用，不分割多字节字符；</td></tr></tbody></table><p>提取范围说明</p><table><thead><tr><th>提取范围</th><th>说明</th></tr></thead><tbody><tr><td>n-</td><td>提取指定第n列或字符或字节后面所有数据</td></tr><tr><td>n-m</td><td>提取指定第n列或字符或字节到第m列或字符或字节中间的所有数据</td></tr><tr><td>-m</td><td>提取指定第m列或字符或字节前面所有数据</td></tr><tr><td>n1,n2,...</td><td>提前指定枚举列的所有数据</td></tr></tbody></table><p>cut切割提取列</p><blockquote><p>cut 文件或数据 -d 分隔符切割 -f 提取第X列</p></blockquote><p>cut切割提取字符</p><blockquote><p>cut 文件或数据 -c 提取字符范围</p></blockquote><p>cut切割提取字节</p><blockquote><p>cut 文件或数据 -nb 提取直接范围</p></blockquote><h3 id="示例-切割提取指定列数据" tabindex="-1">示例：切割提取指定列数据 <a class="header-anchor" href="#示例-切割提取指定列数据" aria-label="Permalink to &quot;示例：切割提取指定列数据&quot;">​</a></h3><p>cut1.txt文件数据准备</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">touch</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span></span></code></pre></div><p>编辑文件添加内容</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">AA</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">itheima</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">11</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">XX</span></span>
<span class="line"><span style="color:#FFCB6B;">BB</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">itcast</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">22</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">XXX</span></span>
<span class="line"><span style="color:#FFCB6B;">CC</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">Shell</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">33</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">XXXX</span></span>
<span class="line"><span style="color:#FFCB6B;">DD</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">it</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">44</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">XXXXXXX</span></span></code></pre></div><p>提取文件中第一列数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 按照空格分隔获取第一列，-d切割列</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -d &quot; &quot;：指定字段的分隔符为空格，表示以空格作为字段分隔符</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -f 1：指定要提取的字段为第1个字段，表示只提取以空格分隔的第一个单词</span></span>
<span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120073.png" alt="image-20200707230710596"></p><p>提取文件中第一列,第三列, 枚举查找</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">,3</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120088.png" alt="image-20200707232926482"></p><p>提取文件中第二列,第三列,第四列, 范围查找</p><div class="language-sh"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;">-4</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242253751.png" alt="image-20220924225317571" style="zoom:80%;"><p>提取文件中第一列后面所有列的数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span><span style="color:#C3E88D;">-</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242254786.png" alt="image-20220924225409604" style="zoom:80%;"><p>提起文件中结束列前面所有列的数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -2 提取指定列前面所有列数据</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242254188.png" alt="image-20220924225449025" style="zoom:80%;"><h3 id="示例-切割提取指定字符数据" tabindex="-1">示例: 切割提取指定字符数据 <a class="header-anchor" href="#示例-切割提取指定字符数据" aria-label="Permalink to &quot;示例: 切割提取指定字符数据&quot;">​</a></h3><p>提取每行前3个字符</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-c1-3</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120089.png" alt="image-20200711142653293"></p><p>提取每行第4个字符以后的数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-c</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">4</span><span style="color:#C3E88D;">-</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120794.png" alt="image-20200711143324756"></p><p>提取每行第3个字符前面所有字符</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-c</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-3</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120799.png" alt="image-20200711143438631"></p><h3 id="示例-切割提取指定字节数据" tabindex="-1">示例：切割提取指定字节数据 <a class="header-anchor" href="#示例-切割提取指定字节数据" aria-label="Permalink to &quot;示例：切割提取指定字节数据&quot;">​</a></h3><p>提取字符串&quot;abc传智播客&quot; 前3个字节</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc传智播客</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-b</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-3</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120807.png" alt="image-20200711143706340"></p><p>提取字符串&quot;abc传智播客&quot; 前4个字节</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc传智播客</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-b</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-4</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120808.png" alt="image-20200711143754196"></p><p>提取字符串&quot;abc传智播客&quot; 前6个字节</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc传智播客</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-b</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-6</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 由于linux系统默认utf-8码表, 所以一个汉字占3个字节</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120811.png" alt="image-20200711143926461"></p><p>提取字符串&quot;abc传智播客&quot; 前4个字节, 就可以将汉字 &quot;传&quot;输出,</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc传智播客</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-nb</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-4</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#  -n 取消多字节字符分割直接输出</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120813.png" alt="image-20200711144139142"></p><h3 id="示例-切割提取指定单词数据" tabindex="-1">示例：切割提取指定单词数据 <a class="header-anchor" href="#示例-切割提取指定单词数据" aria-label="Permalink to &quot;示例：切割提取指定单词数据&quot;">​</a></h3><p>在cut1.txt文件中切割出&quot;itheima&quot;</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 获取itheima所在的哪一行数据，按照空格切割第二列</span></span>
<span class="line"><span style="color:#FFCB6B;">cat</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">cut1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">grep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">itheima</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120461.png" alt="image-20200708003059162"></p><h3 id="示例-切割提取bash进程的pid号" tabindex="-1">示例：切割提取bash进程的PID号 <a class="header-anchor" href="#示例-切割提取bash进程的pid号" aria-label="Permalink to &quot;示例：切割提取bash进程的PID号&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ps</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-aux</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">grep</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">bash</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">head</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">6</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250620537.png" alt="image-20220925062031309" style="zoom:80%;"><h3 id="示例-切割提取ip地址⭐" tabindex="-1">示例：切割提取IP地址⭐ <a class="header-anchor" href="#示例-切割提取ip地址⭐" aria-label="Permalink to &quot;示例：切割提取IP地址⭐&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 找到broadcast所在的行，再去切割第10列，就是数前面空格，inet是第9列</span></span>
<span class="line"><span style="color:#FFCB6B;">ifconfig</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">grep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">broadcast</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">10</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242257936.png" alt="image-20220924225713743" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242257438.png" alt="image-20220924225753275" style="zoom:80%;"><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209242258881.png" alt="image-20220924225822711" style="zoom:80%;"><h2 id="sed文本增删改" tabindex="-1">sed文本增删改 <a class="header-anchor" href="#sed文本增删改" aria-label="Permalink to &quot;sed文本增删改&quot;">​</a></h2><blockquote><p>sed （stream editor, 流编辑器） 是Linux下一款功能强大的非交互流式文本编辑器(vim是交互式文本编辑器)，可以对文本文件的每一行数据匹配查询之后进行增、删、改、查等操作，支持按行、按字段、按正则匹配文本内容，灵活方便，特别适合于大文件的编辑.</p></blockquote><blockquote><p>sed是一种流编辑器，它一次处理一行内容, 将这行放入缓存(存区空间称为：模式空间)，然后才对这行进行处理，处理完后，将缓存区的内容发送到终端。</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120475.png" alt="image-20200711095515087" style="zoom:80%;"><h3 id="语法-1" tabindex="-1">语法 <a class="header-anchor" href="#语法-1" aria-label="Permalink to &quot;语法&quot;">​</a></h3><div class="language-apl"><button title="Copy Code" class="copy"></button><span class="lang">apl</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">sed </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">选项参数</span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">模式匹配</span><span style="color:#89DDFF;">/</span><span style="color:#A6ACCD;">sed程序命令</span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;">文件名</span><span style="color:#89DDFF;">]</span></span></code></pre></div><blockquote><p>模式匹配,sed会读取每一行数据到模式空间中, 之后判断当前行是否符合模式匹配要求,符合要求就会 执行sed程序命令, 否则不会执行sed程序命令;<strong>如果不写匹配模式,那么每一行都会执行sex程序命令</strong></p></blockquote><p>选项参数说明</p><table><thead><tr><th>选项参 数</th><th>功能</th></tr></thead><tbody><tr><td><code>-e</code></td><td>直接在指令列模式上进行sed的动作编辑。它告诉sed将下一个参数解释为一个sed指令，只有当命令行上给出多个sed指令时才需要使用-e选项;一行命令语句可以执行多条sed命令</td></tr><tr><td><code>-i</code></td><td>直接对内容进行修改，不加-i时默认只是预览，不会对文件做实际修改</td></tr><tr><td><code>-f</code></td><td>后跟保存了sed指令的文件</td></tr><tr><td><code>-n</code></td><td>取消默认输出，sed默认会输出所有文本内容，使用-n参数后只显示处理过的行</td></tr><tr><td><code>-r ruguler </code></td><td>使用扩展正则表达式，默认情况sed只识别基本正则表达式 *</td></tr></tbody></table><p>sed程序命令功能描述</p><table><thead><tr><th>命令</th><th>功能描述</th></tr></thead><tbody><tr><td><code>a</code></td><td>add新增，a的后面可以接字串，在下一行出现</td></tr><tr><td><code>c</code></td><td>change更改, 更改匹配行的内容</td></tr><tr><td>d</td><td>delete删除, 删除匹配的内容</td></tr><tr><td><code>i</code></td><td>insert插入, 向匹配行前插入内容</td></tr><tr><td><code>p</code></td><td>print打印, 打印出匹配的内容，通常与-n选项和用</td></tr><tr><td>s</td><td>substitute替换, 替换掉匹配的内容</td></tr><tr><td><code>=</code></td><td>用来打印被匹配的行的行号</td></tr><tr><td><code>n</code></td><td>读取下一行，遇到n时会自动跳入下一行</td></tr></tbody></table><p>特殊符号</p><h3 id="数据准备" tabindex="-1">数据准备 <a class="header-anchor" href="#数据准备" aria-label="Permalink to &quot;数据准备&quot;">​</a></h3><p>sed.txt文件内容</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ABC</span></span>
<span class="line"><span style="color:#FFCB6B;">itheima</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">itheima</span></span>
<span class="line"><span style="color:#FFCB6B;">itcast</span></span>
<span class="line"><span style="color:#FFCB6B;">123</span></span>
<span class="line"><span style="color:#FFCB6B;">itheima</span></span></code></pre></div><h3 id="示例-向文件中添加数据" tabindex="-1">示例：向文件中添加数据 <a class="header-anchor" href="#示例-向文件中添加数据" aria-label="Permalink to &quot;示例：向文件中添加数据&quot;">​</a></h3><h4 id="演示1-指定行号的前或后面添加数据" tabindex="-1">演示1: 指定行号的前或后面添加数据 <a class="header-anchor" href="#演示1-指定行号的前或后面添加数据" aria-label="Permalink to &quot;演示1: 指定行号的前或后面添加数据&quot;">​</a></h4><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 向第三行后面添加hello(未修改原文件，只进行预览)</span></span>
<span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">3ahello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 向第三行后面添加hello(修改完源文件)</span></span>
<span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-i</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">3ahello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><blockquote><p>3 , 代表第三行</p><p>a, 代表在后面添加, 出现在下一行</p><p>注意这里没有修改源文件</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250913708.png" alt="image-20220925091337508" style="zoom:80%;"><p>向第三行<strong>前面</strong>添加hello</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">3ihello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><blockquote><p>3 , 代表第三行</p><p>i, 代表在前面添加, 出现在上一行</p><p>注意这里没有修改源文件，加上-i才能修改</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250914442.png" alt="image-20220925091413246" style="zoom:80%;"><h4 id="演示2-指定内容前或后面添加数据" tabindex="-1">演示2: 指定内容前或后面添加数据 <a class="header-anchor" href="#演示2-指定内容前或后面添加数据" aria-label="Permalink to &quot;演示2: 指定内容前或后面添加数据&quot;">​</a></h4><p>向内容 <code>itheima</code> 后面添加 <code>hello</code> ，如果文件中有多行包括 ``itheima<code></code>，则每一行后面都会添加</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/itheima/ahello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250915296.png" alt="image-20220925091551110" style="zoom:80%;"><p>向内容 <code>itheima</code> 前面添加 <code>hello</code> ，如果文件中有多行包括 ``itheima<code></code>，则每一行前面都会添加</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/itheima/ihello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120040.png" alt="image-20200710001012028"></p><h4 id="演示3-在最后一行前或后添加hello" tabindex="-1">演示3: 在最后一行前或后添加hello <a class="header-anchor" href="#演示3-在最后一行前或后添加hello" aria-label="Permalink to &quot;演示3: 在最后一行前或后添加hello&quot;">​</a></h4><p>在最后一行后面添加hello</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">$ahello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><blockquote><p>$a: 最后一行后面添加</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250917712.png" alt="image-20220925091747518" style="zoom:80%;"><p>在最后一行前面添加hello</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">$ihello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><blockquote><p>$i: 最后一行前面添加</p></blockquote><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250918804.png" alt="image-20220925091816604" style="zoom:80%;"><h3 id="示例-删除文件中的数据" tabindex="-1">示例: 删除文件中的数据 <a class="header-anchor" href="#示例-删除文件中的数据" aria-label="Permalink to &quot;示例: 删除文件中的数据&quot;">​</a></h3><h4 id="演示1-删除第2行" tabindex="-1">演示1: 删除第2行 <a class="header-anchor" href="#演示1-删除第2行" aria-label="Permalink to &quot;演示1: 删除第2行&quot;">​</a></h4><p>命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">2d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># d 用于删除</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 2d 删除第2行</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250918694.png" alt="image-20220925091851493" style="zoom:80%;"><p>命令: 删除第1行,第4行数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1d;4d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250919375.png" alt="image-20220925091926174" style="zoom:80%;"><h4 id="演示2-删除奇数行" tabindex="-1">演示2: 删除奇数行 <a class="header-anchor" href="#演示2-删除奇数行" aria-label="Permalink to &quot;演示2: 删除奇数行&quot;">​</a></h4><p>从第一行开始删除，每隔2行就删掉一行</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1~2d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 1~2 从第1行开始, 每隔2行删一次</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250920568.png" alt="image-20220925092006404" style="zoom:80%;"><h4 id="演示3-删除指定范围的多行数据" tabindex="-1">演示3: 删除指定范围的多行数据 <a class="header-anchor" href="#演示3-删除指定范围的多行数据" aria-label="Permalink to &quot;演示3:  删除指定范围的多行数据&quot;">​</a></h4><p>删除从第1行到第3行的数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1,3d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 1,3  从指定第1行开始到第3行结束</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250920446.png" alt="image-20220925092037259" style="zoom:80%;"><h4 id="演示3-删除指定范围取反的多行数据" tabindex="-1">演示3: 删除指定范围取反的多行数据 <a class="header-anchor" href="#演示3-删除指定范围取反的多行数据" aria-label="Permalink to &quot;演示3:  删除指定范围取反的多行数据&quot;">​</a></h4><p>删除从第1行到第3行取反的数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1,3!d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 1,3! 从指定第1行开始到第3行结束取反, 就是不在这个范围的行</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120743.png" alt="image-20200710171410645"></p><h4 id="演示4-删除最后一行" tabindex="-1">演示4: 删除最后一行 <a class="header-anchor" href="#演示4-删除最后一行" aria-label="Permalink to &quot;演示4: 删除最后一行&quot;">​</a></h4><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">$d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120750.png" alt="image-20200710173137962"></p><h4 id="演示5-删除匹配itheima的行" tabindex="-1">演示5: 删除匹配itheima的行 <a class="header-anchor" href="#演示5-删除匹配itheima的行" aria-label="Permalink to &quot;演示5: 删除匹配itheima的行&quot;">​</a></h4><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/itheima/d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120751.png" alt="image-20200710173601573"></p><h4 id="演示6-删除匹配行到最后一行" tabindex="-1">演示6: 删除匹配行到最后一行 <a class="header-anchor" href="#演示6-删除匹配行到最后一行" aria-label="Permalink to &quot;演示6: 删除匹配行到最后一行&quot;">​</a></h4><p>删除匹配itheima行到最后一行 , 命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/itheima/,$d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># , 代表范围匹配</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120760.png" alt="image-20200710174149348"></p><h4 id="演示7-删除匹配行及其后面一行" tabindex="-1">演示7: 删除匹配行及其后面一行 <a class="header-anchor" href="#演示7-删除匹配行及其后面一行" aria-label="Permalink to &quot;演示7: 删除匹配行及其后面一行&quot;">​</a></h4><p>删除匹配itheima行及其后面一行</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/itheima/,+1d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120290.png" alt="image-20200710174655691"></p><h4 id="演示9-删除不匹配的行" tabindex="-1">演示9: 删除不匹配的行 <a class="header-anchor" href="#演示9-删除不匹配的行" aria-label="Permalink to &quot;演示9: 删除不匹配的行&quot;">​</a></h4><p>删除不匹配 <code>itheima</code> 或 <code>itcast</code> 的行</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/itheima\|itcast/!d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># \| 是正则表达式的或者 这里|需要转义, 所以为\|</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># ! 取反</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120300.png" alt="image-20200711105205862"></p><h3 id="示例-更改文件中的数据" tabindex="-1">示例：更改文件中的数据 <a class="header-anchor" href="#示例-更改文件中的数据" aria-label="Permalink to &quot;示例：更改文件中的数据&quot;">​</a></h3><h4 id="演示1-将文件的第一行修改为hello" tabindex="-1">演示1:将文件的第一行修改为hello <a class="header-anchor" href="#演示1-将文件的第一行修改为hello" aria-label="Permalink to &quot;演示1:将文件的第一行修改为hello&quot;">​</a></h4><p>命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1chello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120302.png" alt="image-20200710110433788"></p><h4 id="演示2-将包含itheima的行修改为hello" tabindex="-1">演示2: 将包含itheima的行修改为hello <a class="header-anchor" href="#演示2-将包含itheima的行修改为hello" aria-label="Permalink to &quot;演示2: 将包含itheima的行修改为hello&quot;">​</a></h4><p>命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/itheima/chello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120305.png" alt="image-20200710110651523"></p><h4 id="演示3-将最后一行修改为hello" tabindex="-1">演示3: 将最后一行修改为hello <a class="header-anchor" href="#演示3-将最后一行修改为hello" aria-label="Permalink to &quot;演示3: 将最后一行修改为hello&quot;">​</a></h4><p>命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">$chello</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120312.png" alt="image-20200710111014651"></p><h4 id="演示4-将文件中的itheima替换为hello" tabindex="-1">演示4: 将文件中的itheima替换为hello <a class="header-anchor" href="#演示4-将文件中的itheima替换为hello" aria-label="Permalink to &quot;演示4: 将文件中的itheima替换为hello&quot;">​</a></h4><p>将文件中的itheima替换为hello,默认只替换每行第一个itheima</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/itheima/hello/</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120315.png" alt="image-20200710154359291"></p><blockquote><p>注意 <code>&#39;s/itheima/hello/&#39;</code> 最后一个<code>/</code> 不可少</p></blockquote><p>将文本中所有的itheima都替换为hello, 全局替换</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/itheima/hello/g</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">  </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># g 代表匹配全局所有符合的字符</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120244.png" alt="image-20200710154642832"></p><h4 id="演示5-将每行中第二个匹配替换" tabindex="-1">演示5: 将每行中第二个匹配替换 <a class="header-anchor" href="#演示5-将每行中第二个匹配替换" aria-label="Permalink to &quot;演示5: 将每行中第二个匹配替换&quot;">​</a></h4><p>将每行中第二个匹配的itheima替换为hello 命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/itheima/hello/2</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;">   </span><span style="color:#C3E88D;">sex.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120251.png" alt="image-20200710154829235"></p><h4 id="演示6-替换后的内容写入文件" tabindex="-1">演示6: 替换后的内容写入文件 <a class="header-anchor" href="#演示6-替换后的内容写入文件" aria-label="Permalink to &quot;演示6: 替换后的内容写入文件&quot;">​</a></h4><p>将每行中第二个匹配的itheima替换为hello , 将替换后的内容写入到sed2.txt文件中</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 第一种方式</span></span>
<span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/itheima/hello/2pw sed2.txt</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># w写入</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># p打印, -n只是获取</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 第二种方式</span></span>
<span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/itheima/hello/2p </span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed2.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120254.png" alt="image-20200710161127555"></p><h4 id="演示7-正则表达式匹配替换" tabindex="-1">演示7: 正则表达式匹配替换 <a class="header-anchor" href="#演示7-正则表达式匹配替换" aria-label="Permalink to &quot;演示7: 正则表达式匹配替换&quot;">​</a></h4><p>匹配有 <code>i</code> 的行，替换匹配行中 <code>t</code> 后的所有内容为空</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/i/s/t.*//g</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># /t.*/ 表示逗号后的所又内容</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120261.png" alt="image-20200710162013813"></p><h4 id="演示8-每行末尾拼接test" tabindex="-1">演示8: 每行末尾拼接test <a class="header-anchor" href="#演示8-每行末尾拼接test" aria-label="Permalink to &quot;演示8: 每行末尾拼接test&quot;">​</a></h4><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/$/&amp; test</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &amp; 用于拼接</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120265.png" alt="image-20200710164435499"></p><h4 id="演示9-每行行首添加注释" tabindex="-1">演示9: 每行行首添加注释 <code>#</code> <a class="header-anchor" href="#演示9-每行行首添加注释" aria-label="Permalink to &quot;演示9: 每行行首添加注释 `#`&quot;">​</a></h4><p>命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/^/#/</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120264.png" alt="image-20200711111533278"></p><h3 id="示例-查询文件或管道中的数据" tabindex="-1">示例: 查询文件或管道中的数据 <a class="header-anchor" href="#示例-查询文件或管道中的数据" aria-label="Permalink to &quot;示例: 查询文件或管道中的数据&quot;">​</a></h3><h4 id="需求1-查询含有-itcast-的行数据" tabindex="-1">需求1: 查询含有 <code>itcast</code> 的行数据 <a class="header-anchor" href="#需求1-查询含有-itcast-的行数据" aria-label="Permalink to &quot;需求1: 查询含有  `itcast`  的行数据&quot;">​</a></h4><p>命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/itcast/p</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120072.png" alt="image-20200711113348831"></p><h4 id="需求2-管道过滤查询" tabindex="-1">需求2: 管道过滤查询 <a class="header-anchor" href="#需求2-管道过滤查询" aria-label="Permalink to &quot;需求2: 管道过滤查询&quot;">​</a></h4><p>管道查询所有进程中含有sshd的进程信息命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ps</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-aux</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/sshd/p</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250928431.png" alt="image-20220925092846205" style="zoom:80%;"><h3 id="示例-多个sed程序命令执行" tabindex="-1">示例: 多个sed程序命令执行 <a class="header-anchor" href="#示例-多个sed程序命令执行" aria-label="Permalink to &quot;示例: 多个sed程序命令执行&quot;">​</a></h3><p>将sed.txt文件中的第1行删除并将 <code>itheima</code> 替换为 <code>itcast</code></p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 第一种方式, 多个sed程序命令 在每个命令之前使用 -e 参数</span></span>
<span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-e</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-e</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/itheima/itcast/g</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 第二种方式</span></span>
<span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1d;s/itheima/itcast/g</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120078.png" alt="image-20200711105707546"></p><h3 id="sed高级用法-缓存区数据交换" tabindex="-1">sed高级用法: 缓存区数据交换 <a class="header-anchor" href="#sed高级用法-缓存区数据交换" aria-label="Permalink to &quot;sed高级用法: 缓存区数据交换&quot;">​</a></h3><h4 id="模式空间与暂存空间介绍" tabindex="-1">模式空间与暂存空间介绍 <a class="header-anchor" href="#模式空间与暂存空间介绍" aria-label="Permalink to &quot;模式空间与暂存空间介绍&quot;">​</a></h4><ol><li><p>首先需要明白, sed处理文件是逐行处理的, 即<strong>读取一行处理一行,输出一行</strong>;</p></li><li><p>sed把文件读出来每一行存放的空间叫模式空间, 会在该空间中对读到的内容做相应处理;</p></li><li><p>此外sed还有一个额外的空间即暂存空间, 暂存空间刚开始里边只有个空行, 记住这一点;</p></li><li><p>sed可使用相应的命令从模式空间往暂存空间放入内容或从暂存空间取内容放入模式空间;</p><blockquote><p>2个缓存空间传输数据的目的是为了更好的处理数据, 一会参考案例学习</p></blockquote></li></ol><h4 id="关于缓存区sed程度命令" tabindex="-1">关于缓存区sed程度命令 <a class="header-anchor" href="#关于缓存区sed程度命令" aria-label="Permalink to &quot;关于缓存区sed程度命令&quot;">​</a></h4><table><thead><tr><th>命令</th><th>含义</th></tr></thead><tbody><tr><td>h</td><td>将<strong>模式空间</strong>里面的内容复制到<strong>暂存空间</strong>缓存区(覆盖方式)</td></tr><tr><td>H</td><td>将<strong>模式空间</strong>里面的内容复制到<strong>暂存空间</strong>缓存区(追加方式)</td></tr><tr><td>g</td><td>将<strong>暂存空间</strong>里面的内容复制到<strong>模式空间</strong>缓存区(覆盖方式)</td></tr><tr><td>G</td><td>将<strong>暂存空间</strong>里面的内容复制到<strong>模式空间</strong>缓存区(追加方式)</td></tr><tr><td>x</td><td>交换2个空间的内容</td></tr></tbody></table><h3 id="示例-缓存空间数据交换" tabindex="-1">示例: 缓存空间数据交换 <a class="header-anchor" href="#示例-缓存空间数据交换" aria-label="Permalink to &quot;示例: 缓存空间数据交换&quot;">​</a></h3><h4 id="演示1-第一行粘贴到最后1行" tabindex="-1">演示1: 第一行粘贴到最后1行 <a class="header-anchor" href="#演示1-第一行粘贴到最后1行" aria-label="Permalink to &quot;演示1: 第一行粘贴到最后1行&quot;">​</a></h4><p>将模式空间第一行复制到暂存空间(覆盖方式),并将暂存空间的内容复制到模式空间中的最后一行(追加方式)</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1h;$G</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 1h 从模式空间中将第一行数据复制到暂存空间(覆盖方式)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># $G 将暂存空间中的内容复制到模式空间中最后一行(追加方式)</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120082.png" alt="image-20200711103556835"></p><h4 id="演示2-第一行删除后粘贴到最后1行" tabindex="-1">演示2: 第一行删除后粘贴到最后1行 <a class="header-anchor" href="#演示2-第一行删除后粘贴到最后1行" aria-label="Permalink to &quot;演示2: 第一行删除后粘贴到最后1行&quot;">​</a></h4><p>将模式空间第一行复制到暂存空间(覆盖方式)并删除, 最后将暂存空间的内容复制到模式空间中的最后一行(追加方式)</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1{h;d};$G</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 1{h;d}对模式空间中的第一行数据同时进行复制到暂存空间(覆盖方式)和删除模式空间中的第一行数据</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120087.png" alt="image-20200711103901519"></p><h4 id="演示3-第一行数据复制粘贴替换其他行数据" tabindex="-1">演示3: 第一行数据复制粘贴替换其他行数据 <a class="header-anchor" href="#演示3-第一行数据复制粘贴替换其他行数据" aria-label="Permalink to &quot;演示3: 第一行数据复制粘贴替换其他行数据&quot;">​</a></h4><p>将模式空间第一行复制到暂存空间(覆盖方式), 最后将暂存空间的内容复制到模式空间中替换从第2行开始到最后一行的每一行数据(覆盖方式)</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1h;2,$g</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120089.png" alt="image-20200711104451987"></p><h4 id="演示4-将前3行数据数据复制粘贴到最后一行" tabindex="-1">演示4: 将前3行数据数据复制粘贴到最后一行 <a class="header-anchor" href="#演示4-将前3行数据数据复制粘贴到最后一行" aria-label="Permalink to &quot;演示4: 将前3行数据数据复制粘贴到最后一行&quot;">​</a></h4><p>将前3行数据复制到暂存空间(追加方式), 之后将暂存空间的所有内容复制粘贴到模式空间最后一行(追加方式)</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">1,3H;$G</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120139.png" alt="image-20200711104856968"></p><h3 id="示例-给每一行添加空行" tabindex="-1">示例: 给每一行添加空行 <a class="header-anchor" href="#示例-给每一行添加空行" aria-label="Permalink to &quot;示例: 给每一行添加空行&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">G</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-i</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># G 每行后面添加一个空行</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -i 修改源文件</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120145.png" alt="image-20200711095724616"></p><h3 id="示例-删除所有的空行" tabindex="-1">示例: 删除所有的空行 <a class="header-anchor" href="#示例-删除所有的空行" aria-label="Permalink to &quot;示例: 删除所有的空行&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-i</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/^$/d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120151.png" alt="image-20200711095844232"></p><h2 id="awk文本分析" tabindex="-1">awk文本分析 <a class="header-anchor" href="#awk文本分析" aria-label="Permalink to &quot;awk文本分析&quot;">​</a></h2><blockquote><p>awk是一个强大的文本分析工具，相对于grep的查找，sed的编辑，awk在其对数据分析并生成报告时,显得尤为强大简单来说awk就是把文件逐行的读入，以空格为默认分隔符将每行切片，切开的部分再进行各种分析处理, 因为切开的部分使用awk可以定义变量,运算符, 使用流程控制语句进行深度加工与分析。</p></blockquote><blockquote><p>创始人 Alfred V. <strong>A</strong>ho、Peter J. <strong>W</strong>einberger和Brian W. <strong>K</strong>ernighan awk由来是姓氏的首字母</p></blockquote><h3 id="语法-2" tabindex="-1">语法 <a class="header-anchor" href="#语法-2" aria-label="Permalink to &quot;语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> [options] </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">pattern{action}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">{</span><span style="color:#FFCB6B;">filenames}</span></span></code></pre></div><blockquote><p>pattern：表示AWK在数据中查找的内容，就是匹配模式</p><p>action：在找到匹配内容时所执行的一系列命令</p></blockquote><p>选项参数说明</p><table><thead><tr><th>选项参数</th><th>功能</th></tr></thead><tbody><tr><td>-F</td><td>指定输入文件拆分分隔符</td></tr><tr><td>-v</td><td>赋值一个用户定义变量</td></tr></tbody></table><h3 id="awk内置变量" tabindex="-1">awk内置变量 <a class="header-anchor" href="#awk内置变量" aria-label="Permalink to &quot;awk内置变量&quot;">​</a></h3><table><thead><tr><th>内置变量</th><th>含义</th></tr></thead><tbody><tr><td>ARGC</td><td>命令行参数个数</td></tr><tr><td>ARGV</td><td>命令行参数排列</td></tr><tr><td>ENVIRON</td><td>支持队列中系统环境变量的使用</td></tr><tr><td>FILENAME</td><td>awk浏览的文件名</td></tr><tr><td>FNR</td><td>浏览文件的记录数</td></tr><tr><td>FS</td><td>设置输入域分隔符，等价于命令行 -F选项</td></tr><tr><td>NF</td><td>浏览记录的域的个数, 根据分隔符分割后的列数</td></tr><tr><td>NR</td><td>已读的记录数, 也是行号</td></tr><tr><td>OFS</td><td>输出域分隔符</td></tr><tr><td>ORS</td><td>输出记录分隔符</td></tr><tr><td>RS</td><td>控制记录分隔符</td></tr><tr><td><code>$n</code></td><td><code>$0</code>变量是指整条记录。<code>$1</code>表示当前行的第一个域,<code>$2</code>表示当前行的第二个域,......以此类推。</td></tr><tr><td>$NF</td><td>$NF是number finally,表示最后一列的信息，跟变量NF是有区别的，变量NF统计的是每行列的总数</td></tr></tbody></table><h3 id="数据准备-1" tabindex="-1">数据准备 <a class="header-anchor" href="#数据准备-1" aria-label="Permalink to &quot;数据准备&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 表示当前目录</span></span>
<span class="line"><span style="color:#FFCB6B;">cp</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/etc/passwd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">./</span></span></code></pre></div><h3 id="示例-默认每行空格切割数据" tabindex="-1">示例 : 默认每行空格切割数据 <a class="header-anchor" href="#示例-默认每行空格切割数据" aria-label="Permalink to &quot;示例 : 默认每行空格切割数据&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># print后的连接符任意</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc 123 456</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{print $1&quot;，&quot;$2&quot;，&quot;$3}</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250957470.png" alt="image-20220925095732237" style="zoom:80%;"><h3 id="示例-打印含有匹配信息的行" tabindex="-1">示例: 打印含有匹配信息的行 <a class="header-anchor" href="#示例-打印含有匹配信息的行" aria-label="Permalink to &quot;示例: 打印含有匹配信息的行&quot;">​</a></h3><p>搜索passwd文件有root关键字的所有行</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/root/</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &#39;/root/&#39; 是查找匹配模式, 没有action命令, 默认输出所有符合的行数据</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250958506.png" alt="image-20220925095818315" style="zoom:80%;"><h3 id="示例-打印匹配行中第7列数据" tabindex="-1">示例: 打印匹配行中第7列数据 <a class="header-anchor" href="#示例-打印匹配行中第7列数据" aria-label="Permalink to &quot;示例: 打印匹配行中第7列数据&quot;">​</a></h3><p>搜索passwd文件有root关键字的所有行, 然后以&quot;:&quot;拆分并打印输出第7列</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/root/{print $7}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -F: 以&#39;:&#39;分隔符拆分每一个列(域)数据</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250959425.png" alt="image-20220925095909176" style="zoom:80%;"><h3 id="示例-打印文件每行属性信息" tabindex="-1">示例: 打印文件每行属性信息 <a class="header-anchor" href="#示例-打印文件每行属性信息" aria-label="Permalink to &quot;示例: 打印文件每行属性信息&quot;">​</a></h3><p>统计passwd: 文件名，每行的行号，每行的列数，对应的完整行内容:</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{print &quot;文件名:&quot; FILENAME &quot;,行号:&quot; NR &quot;,列数:&quot; NF &quot;,内容:&quot; $0}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &quot;文件名:&quot; 用于拼接字符串</span></span></code></pre></div><p>运行效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251000258.png" alt="image-20220925100001918" style="zoom:80%;"><p>使用printf替代print,可以让代码阅读型更好</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{printf(&quot;文件名:%5s,行号:%2s, 列数:%1s, 内容:%2s\n&quot;,FILENAME,NR,NF,$O)}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># printf(格式字符串,变量1,变量2,...)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 格式字符串: %ns 输出字符串,n 是数字，指代输出几个字符, n不指定自动占长度</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 格式字符串: %ni 输出整数,n 是数字，指代输出几个数字</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 格式字符串: %m.nf 输出浮点数,m 和 n 是数字，指代输出的整数位数和小数位数。如 %8.2f 代表共输出 8 位数，其中 2 位是小数，6 位是整数；</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251000016.png" alt="image-20220925100051763" style="zoom:80%;"><h3 id="示例-打印第二行信息" tabindex="-1">示例: 打印第二行信息 <a class="header-anchor" href="#示例-打印第二行信息" aria-label="Permalink to &quot;示例: 打印第二行信息&quot;">​</a></h3><p>打印/etc/passwd/的第二行信息</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">NR==2{printf(&quot;filename:%s,%s\n&quot;,FILENAME,$0)}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120941.png" alt="image-20200711171311183"></p><h3 id="示例-查找以c开头的资源" tabindex="-1">示例: 查找以c开头的资源 <a class="header-anchor" href="#示例-查找以c开头的资源" aria-label="Permalink to &quot;示例: 查找以c开头的资源&quot;">​</a></h3><p>awk过滤的使用, 查找当前目录下文件名以c开头的文件列表</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ls</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/^c/</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120951.png" alt="image-20200711171918200"></p><h3 id="示例-打印第一列" tabindex="-1">示例: 打印第一列 <a class="header-anchor" href="#示例-打印第一列" aria-label="Permalink to &quot;示例: 打印第一列&quot;">​</a></h3><p>按照&quot;:&quot; 分割查询第一列打印输出</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">:</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{print $1}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120965.png" alt="image-20200711172121503"></p><h3 id="示例-打印最后1列" tabindex="-1">示例: 打印最后1列 <a class="header-anchor" href="#示例-打印最后1列" aria-label="Permalink to &quot;示例: 打印最后1列&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 按照&quot;:&quot; 分割查询最后一列打印输出</span></span>
<span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{print $NF}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120969.png" alt="image-20200711172417497"></p><h3 id="示例-打印倒数第二列" tabindex="-1">示例: 打印倒数第二列 <a class="header-anchor" href="#示例-打印倒数第二列" aria-label="Permalink to &quot;示例: 打印倒数第二列&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 按照&quot;:&quot; 分割查询倒数第二列打印输出</span></span>
<span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{print $(NF-1)}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># $(NF-N) N是几, 就是倒数第几列</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120674.png" alt="image-20200711173518580"></p><h3 id="示例-打印10到20行的第一列" tabindex="-1">示例: 打印10到20行的第一列 <a class="header-anchor" href="#示例-打印10到20行的第一列" aria-label="Permalink to &quot;示例: 打印10到20行的第一列&quot;">​</a></h3><p>获取第10到20行的第一列的信息</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F:</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{if(NR&gt;=10 &amp;&amp; NR&lt;=20) print $1}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">passwd</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120683.png" alt="image-20200711173734821"></p><h3 id="示例-多分隔符使用" tabindex="-1">示例: 多分隔符使用 <a class="header-anchor" href="#示例-多分隔符使用" aria-label="Permalink to &quot;示例: 多分隔符使用&quot;">​</a></h3><p>&quot;one:two/three&quot;字符串按照多个分隔符&quot;:&quot;或者&quot;/&quot; 分割, 并打印分割后每个列数据</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">one:two/three</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">[:/]</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{printf(&quot;%s\n%s\n%s\n%s\n&quot;,$0,$1,$2,$3)}</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120685.png" alt="image-20200711174827654"></p><h3 id="示例-添加开始与结束内容" tabindex="-1">示例: 添加开始与结束内容 <a class="header-anchor" href="#示例-添加开始与结束内容" aria-label="Permalink to &quot;示例: 添加开始与结束内容&quot;">​</a></h3><p>给数据添加开始与结束</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-e</span><span style="color:#A6ACCD;">  </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc\nabc</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">BEGIN{print &quot;开始...&quot;} {print $0} END{print &quot;结束...&quot;}</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># BEGIN 在所有数据读取行之前执行；END 在所有数据执行之后执行。</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251005773.png" alt="image-20220925100522531" style="zoom:80%;"><h3 id="示例-使用循环拼接分割后的字符串" tabindex="-1">示例 : 使用循环拼接分割后的字符串 <a class="header-anchor" href="#示例-使用循环拼接分割后的字符串" aria-label="Permalink to &quot;示例 : 使用循环拼接分割后的字符串&quot;">​</a></h3><p>&quot;abc itheima itcast 21&quot; 使用空格分割后, 通过循环拼接在一起</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">abc itheima itcast   21</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-v</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">str=</span><span style="color:#89DDFF;">&quot;&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">[ ]+</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{for(n=1;n&lt;=NF;n++){ str=str$n} print str }</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;"># -v 定义变量</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251006661.png" alt="image-20220925100635377" style="zoom:80%;"><h3 id="示例-操作指定数字运算" tabindex="-1">示例: 操作指定数字运算 <a class="header-anchor" href="#示例-操作指定数字运算" aria-label="Permalink to &quot;示例: 操作指定数字运算&quot;">​</a></h3><p>将passwd文件中的用户id增加数值1并输出</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">2.1</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-v</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">i=</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{print $0+i}</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120697.png" alt="image-20200711215839824"></p><h3 id="示例-切割ip" tabindex="-1">示例: 切割ip <a class="header-anchor" href="#示例-切割ip" aria-label="Permalink to &quot;示例: 切割ip&quot;">​</a></h3><p>切割IP</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ifconfig</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/broadcast/{print}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{print $2}</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120265.png" alt="image-20200711220230406"></p><p>示例: 显示空行行号</p><p>查询sed.txt中空行所在的行号</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">G</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sed.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/^$/{print NR}</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120274.png" alt="image-20200712085616584"></p><h2 id="sort排序" tabindex="-1">sort排序 <a class="header-anchor" href="#sort排序" aria-label="Permalink to &quot;sort排序&quot;">​</a></h2><blockquote><p>sort命令是在Linux里非常有用，它将文件进行排序，并将排序结果标准输出或重定向输出到指定文件。</p></blockquote><h3 id="语法-3" tabindex="-1">语法 <a class="header-anchor" href="#语法-3" aria-label="Permalink to &quot;语法&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> (options) 参数</span></span></code></pre></div><table><thead><tr><th>选项</th><th>说明</th></tr></thead><tbody><tr><td>==-n==</td><td>number,依照数值的大小排序</td></tr><tr><td>==-r==</td><td>reverse, 以相反的顺序来排序</td></tr><tr><td>==-t 分隔字符==</td><td>设置排序时所用的分隔字符, 默认空格是分隔符</td></tr><tr><td>==-k==</td><td>指定需要排序的列</td></tr><tr><td>-d</td><td>排序时，处理英文字母、数字及空格字符外，忽略其他的字符。</td></tr><tr><td>-f</td><td>排序时，将小写字母视为大写字母</td></tr><tr><td>-b</td><td>忽略每行前面开始出的空格字符</td></tr><tr><td>==-o 输出文件==</td><td>将排序后的结果存入指定的文件</td></tr><tr><td>-u</td><td>意味着是唯一的(unique)，输出的结果是去完重了的</td></tr><tr><td>-m</td><td>将几个排序好的文件进行合并</td></tr></tbody></table><p>参数：指定待排序的文件列表</p><h3 id="数据准备-2" tabindex="-1">数据准备 <a class="header-anchor" href="#数据准备-2" aria-label="Permalink to &quot;数据准备&quot;">​</a></h3><p>sort.txt文本文件代码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">张三</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;">  </span></span>
<span class="line"><span style="color:#FFCB6B;">李四</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">95</span><span style="color:#A6ACCD;">  </span></span>
<span class="line"><span style="color:#FFCB6B;">播仔</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">85</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#FFCB6B;">播仔</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">85</span></span>
<span class="line"><span style="color:#FFCB6B;">播仔</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">86</span></span>
<span class="line"><span style="color:#FFCB6B;">AA</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">85</span></span>
<span class="line"><span style="color:#FFCB6B;">播妞</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">100</span></span></code></pre></div><h3 id="示例1-数字升序" tabindex="-1">示例1: 数字升序 <a class="header-anchor" href="#示例1-数字升序" aria-label="Permalink to &quot;示例1: 数字升序&quot;">​</a></h3><p>按照“ ”空格分割后的第2列数字升序排序。</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-k2n,2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sort.txt</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -t &quot; &quot; 代表使用空格分隔符拆分列</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -k 2n,2 代表根据从第2列开始到第2列结束进行数字升序, 仅对第2列排序,n表示升序</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209250936614.png" alt="image-20220925093649404" style="zoom:80%;"><h3 id="示例2-数字升序去重" tabindex="-1">示例2: 数字升序去重 <a class="header-anchor" href="#示例2-数字升序去重" aria-label="Permalink to &quot;示例2: 数字升序去重&quot;">​</a></h3><p>先按照“ ”空格分割后的, 然后,按照第2列数字升序排序, 最后对所有列去重</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-k2n,2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uk1,2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sort.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120283.png" alt="image-20200713012712536" style="zoom:80%;"><blockquote><p>注意: 先排序再去重</p></blockquote><h3 id="示例3-数字升序去重结果保存到文件" tabindex="-1">示例3: 数字升序去重结果保存到文件 <a class="header-anchor" href="#示例3-数字升序去重结果保存到文件" aria-label="Permalink to &quot;示例3: 数字升序去重结果保存到文件&quot;">​</a></h3><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-k2n,2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uk1,2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-o</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sort2.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sort.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120292.png" alt="image-20200713012900639"></p><h3 id="示例4-数字降序去重" tabindex="-1">示例4: 数字降序去重 <a class="header-anchor" href="#示例4-数字降序去重" aria-label="Permalink to &quot;示例4: 数字降序去重&quot;">​</a></h3><p>先按照“ ”空格分割后的, 然后,按照第2列数字降序排序, 最后对所有列去重</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-k2nr,2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-uk1,2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sort.txt</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120294.png" alt="image-20200713013216947"></p><h3 id="示例5-多列排序" tabindex="-1">示例5: 多列排序 <a class="header-anchor" href="#示例5-多列排序" aria-label="Permalink to &quot;示例5: 多列排序&quot;">​</a></h3><p>数据准备sort3.txt</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">公司A,部门A,3</span></span>
<span class="line"><span style="color:#FFCB6B;">公司A,部门B,0</span></span>
<span class="line"><span style="color:#FFCB6B;">公司A,部门C,10</span></span>
<span class="line"><span style="color:#FFCB6B;">公司A,部门D,9</span></span>
<span class="line"><span style="color:#FFCB6B;">公司B,部门A,30</span></span>
<span class="line"><span style="color:#FFCB6B;">公司B,部门B,40</span></span>
<span class="line"><span style="color:#FFCB6B;">公司B,部门C,43</span></span>
<span class="line"><span style="color:#FFCB6B;">公司B,部门D,1</span></span>
<span class="line"><span style="color:#FFCB6B;">公司C,部门A,30</span></span>
<span class="line"><span style="color:#FFCB6B;">公司C,部门B,9</span></span>
<span class="line"><span style="color:#FFCB6B;">公司C,部门C,100</span></span>
<span class="line"><span style="color:#FFCB6B;">公司C,部门D,80</span></span>
<span class="line"><span style="color:#FFCB6B;">公司C,部门E,60</span></span></code></pre></div><p>要求: 以&quot;,&quot;分割先对第一列字符串升序, 再对第3列数字降序</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">,</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-k1,1</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-k3nr,3</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sort3.txt</span></span></code></pre></div><p>运行效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120026.png" alt="image-20200713013821197" style="zoom:80%;"><p>能够使用sort对字符串升序或降序排序</p><blockquote><p>字符串升序: <code>sort -kstart,end 文件</code></p><p>字符串降序: <code>sort -kstartr,end 文件</code></p></blockquote><p>能够使用sort 对数字升序或降序</p><blockquote><p>数字升序: <code>sort -kstartn,end 文件</code></p><p>数字降序: <code>sort -kstartnr,end 文件</code></p></blockquote><p>能够使用sort 对多列进行排序</p><blockquote><p><code>sort -kstart[nr],end -kstart[nr],end ... 文件</code></p></blockquote><h1 id="经典应用⭐⭐" tabindex="-1">经典应用⭐⭐ <a class="header-anchor" href="#经典应用⭐⭐" aria-label="Permalink to &quot;经典应用⭐⭐&quot;">​</a></h1><h2 id="查询-删除空行" tabindex="-1">查询 &amp; 删除空行 <a class="header-anchor" href="#查询-删除空行" aria-label="Permalink to &quot;查询 &amp; 删除空行&quot;">​</a></h2><blockquote><p>问题：使用Linux命令查询file.txt中空行所在的行号，file1.txt数据准备</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">itheima</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">itheima</span></span>
<span class="line"></span>
<span class="line"><span style="color:#FFCB6B;">itcast</span></span>
<span class="line"><span style="color:#FFCB6B;">123</span></span>
<span class="line"></span>
<span class="line"><span style="color:#FFCB6B;">itheima</span></span></code></pre></div><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># awk：命令名称，用于处理文本数据</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># &#39;/^$/{print NR}&#39;：awk程序，包含一个模式和一个动作，用于匹配空行并输出它们所在的行号</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># /^$/：模式，用于匹配空行，即不包含任何字符的行</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># {print NR}：动作，用于输出空行所在的行号（即行号）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># file1.txt：输入文件名</span></span>
<span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/^$/{print NR}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file1.txt</span></span>
<span class="line"><span style="color:#FFCB6B;">sed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-i</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">/^$/d</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file1.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121611925.png" alt="image-20230312161132687" style="zoom:80%;"><h2 id="求一列的和" tabindex="-1">求一列的和 <a class="header-anchor" href="#求一列的和" aria-label="Permalink to &quot;求一列的和&quot;">​</a></h2><p>问题：有文件file2.txt内容如下:</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">张三</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">40</span></span>
<span class="line"><span style="color:#FFCB6B;">李四</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">50</span></span>
<span class="line"><span style="color:#FFCB6B;">王五</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60</span></span></code></pre></div><p>使用Linux命令计算第二列的和并输出</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># {sum+=$2}：awk程序，包含一个动作，用于计算文件中第2个字段的总和。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># {sum+=$2}：动作，用于将每行文本中的第2个字段加到变量sum中。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># END{print &quot;求和: &quot;sum}：awk程序，包含一个动作，用于在处理完所有行后输出结果。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># END：特殊的模式，用于在处理完所有行后执行。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># {print &quot;求和: &quot;sum}：动作，用于输出求和结果。</span></span>
<span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{sum+=$2} END{print &quot;求和: &quot;sum}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file1.txt</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121631057.png" alt="image-20230312163157854" style="zoom:80%;"><h2 id="检查文件是否存在" tabindex="-1">检查文件是否存在 <a class="header-anchor" href="#检查文件是否存在" aria-label="Permalink to &quot;检查文件是否存在&quot;">​</a></h2><p>问题：Shell脚本里如何检查一个文件是否存在？如果不存在该如何处理？</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-e</span><span style="color:#A6ACCD;"> /root/file1.txt </span><span style="color:#89DDFF;">];</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">then</span><span style="color:#A6ACCD;">  </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">文件存在</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">else</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">文件不存在</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121633433.png" alt="image-20230312163310224" style="zoom:80%;"><h2 id="数字排序-求和" tabindex="-1">数字排序 &amp; 求和 <a class="header-anchor" href="#数字排序-求和" aria-label="Permalink to &quot;数字排序 &amp; 求和&quot;">​</a></h2><p>问题：用shell写一个脚本，对文本中无序的一列数字排序</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">张三</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">40</span></span>
<span class="line"><span style="color:#FFCB6B;">李四</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">20</span></span>
<span class="line"><span style="color:#FFCB6B;">王五</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">60</span></span></code></pre></div><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;"># 它会首先读取file1.txt文件中的每一行文本，并按照第二个字段进行排序。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 然后，awk程序会读取排序后的文本，并将每行文本中的第二个字段加到变量sum中，</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 以计算第二个字段的总和。同时，它还会输出排序后的文本，以便查看排序结果</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 最后，它会输出第二个字段的总和，以便查看计算结果。</span></span>
<span class="line"><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-k2,2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file1.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{sum+=$2;print} END{print &quot;求和: &quot;sum}</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121646309.png" alt="image-20230312164649086" style="zoom:80%;"><h2 id="搜索指定目录下文件内容" tabindex="-1">搜索指定目录下文件内容 <a class="header-anchor" href="#搜索指定目录下文件内容" aria-label="Permalink to &quot;搜索指定目录下文件内容&quot;">​</a></h2><p>问题：请用shell脚本写出查找当前文件夹（/root）下所有的文本文件内容中包含有字符”123”的文件名称?</p><p>这段代码是一个shell命令，用于在<code>/root</code>目录及其子目录中查找包含字符串<code>123</code>的文件，并列出这些文件所在的路径，去除重复的路径，并按字典序排序。具体来说，该命令的各个部分含义如下：</p><ul><li><code>grep -r &quot;123&quot; /root</code>：在<code>/root</code>目录及其子目录中递归查找包含字符串<code>123</code>的文件，并将它们的内容输出到标准输出。</li><li><code>|</code>：管道符号，用于将前一个命令的输出作为后一个命令的输入。</li><li><code>cut -d &quot;:&quot; -f 1</code>：将前一个命令的输出作为输入，使用冒号作为分隔符，只保留每行文本的第一个字段。</li><li><code>|</code>：管道符号，用于将前一个命令的输出作为后一个命令的输入。</li><li><code>sort -u</code>：将前一个命令的输出作为输入，去除重复的行，并按字典序排序。</li></ul><blockquote><p>因此，执行该命令后，它会在<code>/root</code>目录及其子目录中递归查找包含字符串<code>123</code>的文件，并将它们的内容输出到标准输出。然后，它会将输出中的每一行文本使用冒号作为分隔符，只保留每行文本的第一个字段，这样就得到了这些文件所在的路径。最后，它会去除重复的路径，并按字典序排序，以便查看这些文件的列表。</p></blockquote><blockquote><p>需要注意的是，这个命令需要以管理员身份运行，因为它会搜索<code>/root</code>目录及其子目录，这是一个只有管理员才能访问的目录。另外，该命令可能会产生大量输出，因此可以使用<code>grep -r &quot;123&quot; /root | wc -l</code>来统计包含字符串<code>123</code>的文件数量。</p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">grep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-r</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">123</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">cut</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-d</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-f</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-u</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121649845.png" alt="image-20230312164914413" style="zoom:67%;"><h2 id="批量生成文件名" tabindex="-1">批量生成文件名 <a class="header-anchor" href="#批量生成文件名" aria-label="Permalink to &quot;批量生成文件名&quot;">​</a></h2><p>问题: 批量生产指定数目的文件,文件名采用&quot;纳秒&quot;命名</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#82AAFF;">read</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-t</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">30</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">请输入创建文件的数目:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">n</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 检测非数字输入，将变量n中所有的数字都替换为空字符串，然后将替换后的结果输出。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 其中，sed命令中的s/[0-9]//g表示将输入文本中所有的数字（0-9）都替换为空字符串，</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># g表示替换所有匹配到的数字，而不仅仅是第一个</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 如果变量n的值为abc123def456，那么执行该命令后，变量test的值就为abcdef，即去除了所有的数字。</span></span>
<span class="line"><span style="color:#A6ACCD;">test</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$(</span><span style="color:#82AAFF;">echo</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">$n</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">|</span><span style="color:#C3E88D;"> </span><span style="color:#FFCB6B;">sed</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">s/[0-9]//g</span><span style="color:#89DDFF;">&#39;)</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -n &quot;$n&quot;：判断变量n的值是否为非空字符串，如果是，则返回true，否则返回false</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -z &quot;$test&quot;：判断变量test的值是否为空字符串，如果是，则返回true，否则返回false</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># -a，表示逻辑与操作，表示只有当两个条件都为true时，整个表达式才为true</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-n</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$n</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-a</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-z</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$test</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;">;i</span><span style="color:#89DDFF;">&lt;</span><span style="color:#A6ACCD;">$n;i</span><span style="color:#89DDFF;">=</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">+</span><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#676E95;font-style:italic;"># $(date +%N)会执行date +%N命令，将其结果作为字符串返回，+%N选项表示输出当前时间的纳秒部分</span></span>
<span class="line"><span style="color:#A6ACCD;">           name</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$(</span><span style="color:#FFCB6B;">date</span><span style="color:#C3E88D;"> +%N</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#676E95;font-style:italic;"># 如果当前目录下不存在名为temp的文件夹，则创建一个名为temp的文件夹</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#676E95;font-style:italic;"># [! -d ./temp] 表示如果当前目录下不存在名为temp的文件夹，则条件成立，</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#676E95;font-style:italic;"># &amp;&amp;表示只有当前一个条件成立时才执行后面的命令，因此，</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#676E95;font-style:italic;"># 如果当前目录下不存在名为temp的文件夹，则执行mkdir -p ./temp，创建一个名为temp的文件夹。</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">!</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-d</span><span style="color:#A6ACCD;"> ./temp </span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;">  </span><span style="color:#FFCB6B;">mkdir</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">./temp</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#FFCB6B;">touch</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">./temp/</span><span style="color:#A6ACCD;">$name</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">创建 </span><span style="color:#A6ACCD;">$name</span><span style="color:#C3E88D;"> 成功!</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;font-style:italic;">done</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">创建失败</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">           </span><span style="color:#82AAFF;">exit</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">fi</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251028909.png" alt="image-20220925102801668" style="zoom:80%;"><h2 id="批量改名" tabindex="-1">批量改名 <a class="header-anchor" href="#批量改名" aria-label="Permalink to &quot;批量改名&quot;">​</a></h2><p>问题: 将/root/temp目录下所有文件名重命名为&quot;旧文件名-递增数字&quot;?</p><p>重命名命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">rename</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">旧文件名</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">新文件名</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">旧文件所在位置</span></span></code></pre></div><p>脚本代码file5.sh</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#A6ACCD;">filenames</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$(</span><span style="color:#FFCB6B;">ls</span><span style="color:#C3E88D;"> /root/temp</span><span style="color:#89DDFF;">)</span></span>
<span class="line"><span style="color:#A6ACCD;">number</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">1</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> name </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> $filenames</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">命令前:%s</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">name</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    newname</span><span style="color:#89DDFF;">=${</span><span style="color:#A6ACCD;">name</span><span style="color:#89DDFF;">}</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">-</span><span style="color:#89DDFF;">&quot;</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">number</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#FFCB6B;">rename</span><span style="color:#A6ACCD;"> $name </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">newname</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">/root/temp/</span><span style="color:#A6ACCD;">*</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">let</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">number++</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;">#每个改名后的文件名后缀数字加1</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">重命名后:%s \n</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">newname</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120696.png" alt="image-20200713091236973"></p><h2 id="批量创建用户" tabindex="-1">批量创建用户 <a class="header-anchor" href="#批量创建用户" aria-label="Permalink to &quot;批量创建用户&quot;">​</a></h2><p>问题: 根据users.txt中提供的用户列表,一个名一行, 批量添加用户到linux系统中</p><p>已知users.txt数据准备</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">user1</span></span>
<span class="line"><span style="color:#FFCB6B;">user2</span></span></code></pre></div><p>知识点分析1: 添加用户命令</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">useradd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">用户名</span></span></code></pre></div><p>知识点分析2: 设置每个用户密码默认密码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">passwd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--stdin</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">用户名</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120701.png" alt="image-20200713092318381"></p><p>面试题答案: 脚本代码file6.sh</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#A6ACCD;">ULIST</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$(</span><span style="color:#FFCB6B;">cat</span><span style="color:#C3E88D;"> /root/users.txt</span><span style="color:#89DDFF;">)</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;">##/root/users.txt  里面存放的是用户名，一个名一行</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> UNAME </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> $ULIST</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#FFCB6B;">useradd</span><span style="color:#A6ACCD;"> $UNAME</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">123456</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">passwd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--stdin</span><span style="color:#A6ACCD;"> $UNAME </span><span style="color:#89DDFF;">&amp;&gt;</span><span style="color:#A6ACCD;">/dev/null</span></span>
<span class="line"><span style="color:#A6ACCD;">     </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> $? </span><span style="color:#89DDFF;">-eq</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$UNAME用户名与密码添加初始化成功</span><span style="color:#C3E88D;">!</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span></code></pre></div><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120715.png" alt="image-20200713093129265"></p><h2 id="筛选单词" tabindex="-1">筛选单词 <a class="header-anchor" href="#筛选单词" aria-label="Permalink to &quot;筛选单词&quot;">​</a></h2><p>问题: 根据给出的数据输出里面单词长度大于3的单词</p><p>数据准备</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">I</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">may</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">not</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">be</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">able</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">change</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">the</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">past,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">but</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">I</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">can</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">learn</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">from</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">it.</span></span></code></pre></div><p>shell脚本file7.sh</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">I may not be able to change the past, but I can learn from it.</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">[ ,.]</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{for(i=1;i&lt;NF;i++){ if(length($i)&gt;3){print $i}}}</span><span style="color:#89DDFF;">&#39;</span></span></code></pre></div><p>运行效果</p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2023.3.30/202303121725858.png" alt="image-20230312172514622" style="zoom:80%;"><h2 id="单词及字母去重排序" tabindex="-1">单词及字母去重排序 <a class="header-anchor" href="#单词及字母去重排序" aria-label="Permalink to &quot;单词及字母去重排序&quot;">​</a></h2><p>问题</p><div class="language-dart"><button title="Copy Code" class="copy"></button><span class="lang">dart</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#F78C6C;">1</span><span style="color:#A6ACCD;">、按单词出现频率降序排序！</span></span>
<span class="line"><span style="color:#F78C6C;">2</span><span style="color:#A6ACCD;">、按字母出现频率降序排序！</span></span></code></pre></div><p>file8.txt 文件内容</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">No.</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">The</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Bible</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">says</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Jesus</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">had</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">compassion2</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">on</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">them</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">for</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">He</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">saw</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">them</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">as</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sheep</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">without</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">shepherd.</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">They</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">were</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">like</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">lost</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sheep,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">lost</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">in</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">their</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">sin.</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">How</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">the</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Lord</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Jesus</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">loved</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">them!</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">He</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">knew</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">they</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">were</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">helpless</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">needed</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">a</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">shepherd.</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">And</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">the</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Good</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Shepherd</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">knew</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">He</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">had</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">come</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">help</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">them.</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">But</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">not</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">just</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">the</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">people</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">way</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">back</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">then.</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">For</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">the</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Lord</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">Jesus</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">knows</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">all</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">about</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">you,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">loves</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">you</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">too,</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">and</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">wants</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">to</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">help</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">you.</span></span></code></pre></div><p>按照单词出现频率降序</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">[,. ]+</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{for(i=1;i&lt;=NF;i++)S[$i]++}END{for(key in S)print S[key],key}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file8.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-rn</span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">head</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120720.png" alt="image-20200713101616727"></p><p>按照字符出现频率降序前10个</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">awk</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-F</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">{for(i=1;i&lt;=NF;i++)S[$i]++}END{for(key in S)print S[key],key}</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">file8.txt</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">sort</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-rn</span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">head</span></span></code></pre></div><p>运行效果</p><p><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.6.30/202208042120593.png" alt="image-20200713101521632"></p><h2 id="扫描网络内存活主机" tabindex="-1">扫描网络内存活主机 <a class="header-anchor" href="#扫描网络内存活主机" aria-label="Permalink to &quot;扫描网络内存活主机&quot;">​</a></h2><p>问题: 扫描192.168.56.1到192.168.56.254之间ip的是否存活, 并输出是否在线?</p><p>服务器ip存活分析</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#FFCB6B;">ping</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">ip地址</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-c</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">2</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 如果ip地址存活发送2个数据包会至少接收返回1个数据包</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251044739.png" alt="image-20220925104451470" style="zoom:80%;"><p>完整脚本代码</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#A6ACCD;">count</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">0</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> i  </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> 192.168.22.{</span><span style="color:#FFCB6B;">1..254}</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 使用ping命令发送2个包测试, 并获取返回接收到包的个数</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># NR==6，第六行，print $4，第四列</span></span>
<span class="line"><span style="color:#A6ACCD;">    receive</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">$(</span><span style="color:#FFCB6B;">ping</span><span style="color:#C3E88D;"> </span><span style="color:#A6ACCD;">$i</span><span style="color:#C3E88D;"> -c </span><span style="color:#F78C6C;">2</span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">awk</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">NR==6{print $4}</span><span style="color:#89DDFF;">&#39;)</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#676E95;font-style:italic;"># 接收返回包大于0 说明主机在线</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">if</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">receive</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-gt</span><span style="color:#A6ACCD;"> </span><span style="color:#F78C6C;">0</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">]</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">then</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;${</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;"> 在线</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#89DDFF;">((</span><span style="color:#A6ACCD;">count</span><span style="color:#89DDFF;">+=</span><span style="color:#F78C6C;">1</span><span style="color:#89DDFF;">))</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">else</span></span>
<span class="line"><span style="color:#A6ACCD;">        </span><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;${</span><span style="color:#A6ACCD;">i</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;"> 不在线</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#A6ACCD;">    </span><span style="color:#89DDFF;font-style:italic;">fi</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">在线服务器有:</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$count</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">个</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.8.30/202208052218149.png" alt="image-20220805221841831" style="zoom:67%;"><h2 id="mysql分库备份" tabindex="-1">MySQL分库备份 <a class="header-anchor" href="#mysql分库备份" aria-label="Permalink to &quot;MySQL分库备份&quot;">​</a></h2><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#A6ACCD;">user</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">root</span><span style="color:#A6ACCD;">      </span><span style="color:#676E95;font-style:italic;">#用户名</span></span>
<span class="line"><span style="color:#A6ACCD;">pass</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;">      </span><span style="color:#676E95;font-style:italic;">#密码</span></span>
<span class="line"><span style="color:#A6ACCD;">backfile</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">/root/mysql/backup</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;">#备份路径</span></span>
<span class="line"><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">!</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-d</span><span style="color:#A6ACCD;"> $backfile </span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">mkdir</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> $backfile </span><span style="color:#676E95;font-style:italic;">#判断是否有备份路径，不存在就创建目录</span></span>
<span class="line"><span style="color:#A6ACCD;">cmd</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">mysql -u</span><span style="color:#A6ACCD;">$user</span><span style="color:#C3E88D;"> -p</span><span style="color:#A6ACCD;">$pass</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;">#登录数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">dump</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">mysqldump -u</span><span style="color:#A6ACCD;">$user</span><span style="color:#C3E88D;"> -p</span><span style="color:#A6ACCD;">$pass</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;">#mysqldump备份参数</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 获取库名列表，2&gt;/dev/null将警告信息输出到/dev/null</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># egrep -v &quot;_schema|mysql，不去备份系统数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">dblist</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">$cmd</span><span style="color:#C3E88D;"> -e </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">show databases;</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">2&gt;</span><span style="color:#C3E88D;">/dev/null </span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">sed</span><span style="color:#C3E88D;"> </span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">d</span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">egrep</span><span style="color:#C3E88D;"> -v </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_schema|mysql|sys</span><span style="color:#89DDFF;">&quot;`</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">需要备份的数据列表:</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $dblist</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">开始备份:</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;">#for循环备份库列表</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> db_name </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> $dblist </span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">正在备份数据库:%s</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">db_name</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#676E95;font-style:italic;">#库名+时间备份打包至指定路径下</span></span>
<span class="line"><span style="color:#A6ACCD;">   $dump $db_name </span><span style="color:#89DDFF;">2&gt;</span><span style="color:#C3E88D;">/dev/null</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">gzip</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;${</span><span style="color:#A6ACCD;">backfile</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">/</span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">db_name</span><span style="color:#89DDFF;">}</span><span style="color:#C3E88D;">_</span><span style="color:#89DDFF;">$(</span><span style="color:#FFCB6B;">date</span><span style="color:#C3E88D;"> +%m%d</span><span style="color:#89DDFF;">)</span><span style="color:#C3E88D;">.sql.gz</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">,备份完成\n</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">全部备份完成!!!</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251234938.png" alt="image-20220925123401683" style="zoom:80%;"><h2 id="mysql分库分表备份" tabindex="-1">MySQL分库分表备份 <a class="header-anchor" href="#mysql分库分表备份" aria-label="Permalink to &quot;MySQL分库分表备份&quot;">​</a></h2><div class="language-sql"><button title="Copy Code" class="copy"></button><span class="lang">sql</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#A6ACCD;">mysql </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">uroot </span><span style="color:#89DDFF;">-</span><span style="color:#A6ACCD;">p123456</span></span>
<span class="line"><span style="color:#A6ACCD;"># 避免去备份系统数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">show databases;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251226326.png" alt="image-20220925122612063" style="zoom:80%;"><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki material-theme-palenight"><code><span class="line"><span style="color:#676E95;font-style:italic;">#!/bin/bash</span></span>
<span class="line"><span style="color:#A6ACCD;">user</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">root</span><span style="color:#A6ACCD;">      </span><span style="color:#676E95;font-style:italic;">#用户名</span></span>
<span class="line"><span style="color:#A6ACCD;">pass</span><span style="color:#89DDFF;">=</span><span style="color:#F78C6C;">123456</span><span style="color:#A6ACCD;">      </span><span style="color:#676E95;font-style:italic;">#密码</span></span>
<span class="line"><span style="color:#A6ACCD;">backfile</span><span style="color:#89DDFF;">=</span><span style="color:#C3E88D;">/root/mysql/backup</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;"># 设置备份路径</span></span>
<span class="line"></span>
<span class="line"><span style="color:#89DDFF;">[</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">!</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">-d</span><span style="color:#A6ACCD;"> $backfile </span><span style="color:#89DDFF;">]</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&amp;&amp;</span><span style="color:#A6ACCD;"> </span><span style="color:#FFCB6B;">mkdir</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">-p</span><span style="color:#A6ACCD;"> $backfile </span><span style="color:#676E95;font-style:italic;">#判断是否有备份路径，如果没有，则创建该路径</span></span>
<span class="line"><span style="color:#A6ACCD;">cmd</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">mysql -u</span><span style="color:#A6ACCD;">$user</span><span style="color:#C3E88D;"> -p</span><span style="color:#A6ACCD;">$pass</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">  </span><span style="color:#676E95;font-style:italic;">#登录数据库</span></span>
<span class="line"><span style="color:#A6ACCD;">dump</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">mysqldump -u</span><span style="color:#A6ACCD;">$user</span><span style="color:#C3E88D;"> -p</span><span style="color:#A6ACCD;">$pass</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;"> </span><span style="color:#676E95;font-style:italic;">#mysqldump备份参数</span></span>
<span class="line"></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 获将包含所有不属于系统保留的数据库名称列表</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># ${cmd} -e &quot;show databases;&quot;用于获取所有数据库的名称列表。</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 2&gt;/dev/null表示将标准错误输出重定向到/dev/null设备，以避免错误信息输出到屏幕上</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># |sed 1d表示使用sed命令删除第一行输出（因为第一行输出是表头信息）</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># |egrep -v &quot;_schema|mysql|sys&quot;表示使用egrep命令过滤掉名称中包含_schema、mysql或sys的数据库</span></span>
<span class="line"><span style="color:#676E95;font-style:italic;"># 这些数据库通常是系统保留的数据库，不需要进行处理。</span></span>
<span class="line"><span style="color:#A6ACCD;">dblist</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#A6ACCD;">$cmd</span><span style="color:#C3E88D;"> -e </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">show databases;</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">2&gt;</span><span style="color:#C3E88D;">/dev/null </span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">sed</span><span style="color:#C3E88D;"> </span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">d</span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">egrep</span><span style="color:#C3E88D;"> -v </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">_schema|mysql|sys</span><span style="color:#89DDFF;">&quot;`</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">需要备份的数据列表:</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> $dblist</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">开始备份:</span><span style="color:#89DDFF;">&quot;</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> db_name </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> $dblist </span><span style="color:#676E95;font-style:italic;">#for循环备份库列表</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">正在备份数据库:%s\n</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">db_name</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">   tables</span><span style="color:#89DDFF;">=</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">mysql</span><span style="color:#C3E88D;"> -u</span><span style="color:#A6ACCD;">$user</span><span style="color:#C3E88D;"> -p</span><span style="color:#89DDFF;">&quot;</span><span style="color:#A6ACCD;">$pass</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> -e </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">use </span><span style="color:#A6ACCD;">$db_name</span><span style="color:#C3E88D;">;show tables;</span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;"> </span><span style="color:#89DDFF;">2&gt;</span><span style="color:#C3E88D;">/dev/null</span><span style="color:#89DDFF;">|</span><span style="color:#FFCB6B;">sed</span><span style="color:#C3E88D;"> </span><span style="color:#F78C6C;">1</span><span style="color:#C3E88D;">d</span><span style="color:#89DDFF;">`</span></span>
<span class="line"><span style="color:#A6ACCD;">   </span><span style="color:#89DDFF;font-style:italic;">for</span><span style="color:#A6ACCD;"> j </span><span style="color:#89DDFF;font-style:italic;">in</span><span style="color:#A6ACCD;"> $tables</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;font-style:italic;">do</span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">正在备份数据库 %s 表 %s </span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">db_name</span><span style="color:#89DDFF;">}</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">j</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">         $dump </span><span style="color:#C3E88D;">-B</span><span style="color:#A6ACCD;"> </span><span style="color:#C3E88D;">--databases</span><span style="color:#A6ACCD;"> $db_name </span><span style="color:#C3E88D;">--tables</span><span style="color:#A6ACCD;"> $j </span><span style="color:#89DDFF;">2&gt;</span><span style="color:#C3E88D;">/dev/null</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&gt;</span><span style="color:#A6ACCD;"> </span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">backfile</span><span style="color:#89DDFF;">}</span><span style="color:#FFCB6B;">/$</span><span style="color:#A6ACCD;">{db_name}</span><span style="color:#FFCB6B;">-$</span><span style="color:#A6ACCD;">{j}</span><span style="color:#FFCB6B;">-</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">date</span><span style="color:#C3E88D;"> +%m%d</span><span style="color:#89DDFF;">`</span><span style="color:#FFCB6B;">.sql</span></span>
<span class="line"><span style="color:#A6ACCD;">         </span><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">,备份完成\n</span><span style="color:#89DDFF;">&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#89DDFF;font-style:italic;">done</span></span>
<span class="line"><span style="color:#A6ACCD;">      </span><span style="color:#82AAFF;">printf</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&#39;</span><span style="color:#C3E88D;">数据库 %s 备份完成\n</span><span style="color:#89DDFF;">&#39;</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">${</span><span style="color:#A6ACCD;">db_name</span><span style="color:#89DDFF;">}</span></span>
<span class="line"><span style="color:#89DDFF;font-style:italic;">done</span></span>
<span class="line"><span style="color:#82AAFF;">echo</span><span style="color:#A6ACCD;"> </span><span style="color:#89DDFF;">&quot;</span><span style="color:#C3E88D;">全部备份完成!!!</span><span style="color:#89DDFF;">&quot;</span></span></code></pre></div><img src="https://edu-8673.oss-cn-beijing.aliyuncs.com/img2022.10.30/202209251227001.png" alt="image-20220925122715694" style="zoom:80%;"></div></div></main><footer class="VPDocFooter" data-v-6b87e69f data-v-37656e44><!--[--><!--]--><!----><nav class="prev-next" data-v-37656e44><div class="pager" data-v-37656e44><a class="pager-link prev" href="/notebook/Linux/Linux%E8%BF%9B%E9%98%B6.html" data-v-37656e44><span class="desc" data-v-37656e44>Previous page</span><span class="title" data-v-37656e44>Linux新特性</span></a></div><div class="pager" data-v-37656e44><a class="pager-link next" href="/notebook/Linux/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC.html" data-v-37656e44><span class="desc" data-v-37656e44>Next page</span><span class="title" data-v-37656e44>实用脚本</span></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    <script>window.__VP_HASH_MAP__=JSON.parse("{\"2、数据库_mysql_mysql面试_基础.md\":\"40da680a\",\"1、学前端_5、小程序_小程序项目.md\":\"60a1629b\",\"1、学前端_4、node_知识篇.md\":\"a7fb500e\",\"1、学前端_2、js_ts_es6 进阶.md\":\"6d07ba10\",\"1、学前端_3、vue_vue3_vue3进阶.md\":\"7ac622b4\",\"5、运维_jenkins.md\":\"929081f8\",\"1、学前端_2、js_ts_typescript.md\":\"875a4aa4\",\"2、数据库_mysql_mysql核心_设计.md\":\"7faf46d1\",\"2、数据库_mysql_mysql核心_基础.md\":\"d8e97f3e\",\"1、学前端_1、html_css_html基础.md\":\"7584d076\",\"1、学前端_5、专题篇_问题篇.md\":\"e893aaa2\",\"2、数据库_mysql_mysql面试_进阶.md\":\"f934806d\",\"3、springboot_运维_原理.md\":\"f4a39db6\",\"2、数据库_influxdb.md\":\"6e1711e1\",\"3、springboot_新特性.md\":\"cdf3e307\",\"mybatis_mybatisplus_jpa.md\":\"8e41681b\",\"1、学前端_5、小程序_小程序优化.md\":\"a2185198\",\"2、数据库_redis_redis基础.md\":\"856df0e0\",\"linux_实用脚本.md\":\"f2299dd5\",\"4、微服务_必备_分布式基础.md\":\"d49863d5\",\"2、数据库_redis_redis优化.md\":\"e66ae32f\",\"4、微服务_springsecurity_进阶篇.md\":\"235a8e9e\",\"5、运维_chatgpt.md\":\"10db3823\",\"2、数据库_mysql_分库分表.md\":\"e1c8a095\",\"start.md\":\"9bc1ff8d\",\"5、运维_github.md\":\"2ec6c735\",\"java学前端_css.md\":\"f11b47f0\",\"1、学前端_5、专题篇_知识篇.md\":\"a463ed8d\",\"linux_软件部署.md\":\"d6722925\",\"2、数据库_neo4j.md\":\"97ad22ac\",\"team.md\":\"ce467a6a\",\"nginx_实战篇.md\":\"7785486e\",\"index.md\":\"8c3ec167\",\"计算机基础_计算机网络_网络基础.md\":\"7a54a85d\",\"1、学前端_4、node_进阶篇.md\":\"60f6db69\",\"java_java集合.md\":\"a049b313\",\"1、学前端_3、vue_vue3_vue3高级.md\":\"614d1516\",\"1、学前端_5、小程序_微信小程序.md\":\"9a4be771\",\"5、运维_netty.md\":\"12ca0278\",\"2、数据库_mysql_mysql核心_运维.md\":\"83f97c16\",\"idea_vs code.md\":\"afdcb593\",\"java学前端_vue3_组件.md\":\"1086884e\",\"idea_chrome.md\":\"4a32afbc\",\"云原生_k8s.md\":\"db58e65a\",\"2、数据库_mysql_mysql核心_进阶.md\":\"61d16dff\",\"ssm_springbatch.md\":\"f799ab4a\",\"三高_分布式.md\":\"db1b8a1b\",\"2、数据库_elasticsearch_1、es基础.md\":\"04d17448\",\"linux_linux基础.md\":\"4b0bf394\",\"idea_idea插件.md\":\"fa86e45a\",\"可视化 _ 监控_可视化大屏.md\":\"004553bd\",\"2、数据库_mongodb_整合.md\":\"3c47d7f4\",\"4、微服务_springsecurity_基础篇.md\":\"534a3401\",\"4、微服务_进阶.md\":\"69095c58\",\"计算机基础_计算机基础_操作系统.md\":\"0f75d113\",\"可视化 _ 监控_zabbix.md\":\"71f2270e\",\"nginx_基础篇.md\":\"c7d8bb50\",\"1、学前端_4、node_项目实战.md\":\"bc5065b8\",\"2、数据库_redis_redis原理.md\":\"5cedf685\",\"可视化 _ 监控_监控基础.md\":\"ac56ce4d\",\"三高_高并发.md\":\"ea9ffc99\",\"2、数据库_redis_redis高级.md\":\"1d5872f6\",\"1、学前端_4、node_基础篇.md\":\"581cc13a\",\"2、数据库_mongodb_基础.md\":\"fb7a0a29\",\"idea_idea基础.md\":\"6f2f9638\",\"4、微服务_必备_sentinel.md\":\"2edfbf6c\",\"2、数据库_elasticsearch_3、es高级.md\":\"ef146606\",\"1、学前端_3、vue_vue3_vue3新语法.md\":\"8afd5409\",\"消息中间件_canal.md\":\"3949163c\",\"ssm_maven.md\":\"2c5e12ed\",\"4、微服务_springsecurity_高级篇.md\":\"882d3ff3\",\"linux_linux进阶.md\":\"188ef7b4\",\"计算机基础_设计模式_uml.md\":\"634ba256\",\"计算机基础_算法_leetcode.md\":\"77162fb9\",\"项目实战_小兔鲜_进阶篇1.md\":\"17c52c81\",\"1、学前端_2、js_ts_es6 基础.md\":\"fda3f18b\",\"项目实战_小兔鲜_进阶篇2.md\":\"a0f23006\",\"软件测试_测试基础.md\":\"8c1060cd\",\"2、数据库_redis_本地缓存.md\":\"00617fe6\",\"nginx_面试篇.md\":\"e3fb373a\",\"mybatis_mybatisplus_mybatis.md\":\"9239e0ad\",\"linux_shell.md\":\"ae53d83b\",\"2、数据库_mysql_mysql核心_优化.md\":\"36230425\",\"项目实战_项目推荐.md\":\"f9d97630\",\"mybatis_mybatisplus_mybatisplus.md\":\"0030fd35\",\"项目实战_百度地图_进阶篇.md\":\"c8b93267\",\"三高_高可用.md\":\"323840c5\",\"java_java新特性.md\":\"22abf56d\",\"软件测试_压力测试.md\":\"9ab44440\",\"java学前端_html_js.md\":\"e0fcd240\",\"2、数据库_redis_redis实战.md\":\"d6daeeab\",\"nginx_进阶篇.md\":\"e6b63195\",\"三高_秒杀.md\":\"3878bb64\",\"5、运维_git.md\":\"0264925c\",\"java_java进阶.md\":\"e79cb5b4\",\"并发 _ 多线程_基础篇.md\":\"7adbfac5\",\"项目实战_百度地图_基础篇.md\":\"8afa5954\",\"java学前端_react.md\":\"3ec827dd\",\"1、学前端_1、html_css_css基础.md\":\"01b56712\",\"项目实战_小兔鲜_基础篇.md\":\"646f5df5\",\"1、学前端_2、js_ts_js 基础.md\":\"cb13e36f\",\"可视化 _ 监控_监控进阶.md\":\"0cdbc292\",\"计算机基础_设计模式_基础篇.md\":\"51617287\",\"计算机基础_数据结构_基础篇.md\":\"b2bfd8d4\",\"项目实战_苍穹外卖_进阶篇.md\":\"48415e41\",\"ssm_spring.md\":\"ab514659\",\"消息中间件_rabbitmq.md\":\"45b1eb28\",\"1、学前端_1、html_css_网页进阶.md\":\"db998248\",\"消息中间件_kafka.md\":\"b747dabf\",\"云原生_docker.md\":\"983c7ba7\",\"4、微服务_必备_分布式锁.md\":\"5af1cf8d\",\"消息中间件_rocketmq.md\":\"d441da85\",\"项目实战_黑马头条_基础篇.md\":\"b05af3a6\",\"ssm_springmvc.md\":\"81b9714f\",\"项目实战_支付.md\":\"1d7407dd\",\"项目实战_黑马头条_进阶篇2.md\":\"bff0015b\",\"项目实战_黑马头条_进阶篇.md\":\"19f18388\",\"java学前端_vue2_组件.md\":\"58c6b1df\",\"3、springboot_基础篇.md\":\"529c66f4\",\"3、springboot_应用篇.md\":\"8b92aa61\",\"项目实战_黑马头条_高级篇.md\":\"227c08c1\",\"1、学前端_5、小程序_uniapp.md\":\"71a282b4\",\"项目实战_云尚办公_基础篇.md\":\"1fe188ba\",\"并发 _ 多线程_并发完善.md\":\"26619c46\",\"1、学前端_2、js_ts_js 进阶.md\":\"657dfb8f\",\"java_java高级.md\":\"23782d1a\",\"java_java基础.md\":\"86d67c77\"}");window.__VP_SITE_DATA__=JSON.parse("{\"lang\":\"en-US\",\"dir\":\"ltr\",\"title\":\"VitePress\",\"description\":\"A VitePress site\",\"base\":\"/notebook/\",\"head\":[],\"appearance\":true,\"themeConfig\":{\"algolia\":{\"appId\":\"DW7O63I9IR\",\"apiKey\":\"f8ed758cdb288a8b06542bc35923c1a1\",\"indexName\":\"notebook\"},\"sidebar\":[{\"text\":\"Java\",\"collapsed\":true,\"items\":[{\"text\":\"Java基础\",\"link\":\"/Java/Java基础\"},{\"text\":\"Java新特性\",\"link\":\"/Java/Java新特性\"},{\"text\":\"Java进阶\",\"link\":\"/Java/Java进阶\"},{\"text\":\"Java集合\",\"link\":\"/Java/Java集合\"},{\"text\":\"Java高级\",\"link\":\"/Java/Java高级\"}]},{\"text\":\"Linux\",\"collapsed\":true,\"items\":[{\"text\":\"Linux基础\",\"link\":\"/Linux/Linux基础\"},{\"text\":\"Linux新特性\",\"link\":\"/Linux/Linux进阶\"},{\"text\":\"Shell脚本\",\"link\":\"/Linux/Shell\"},{\"text\":\"实用脚本\",\"link\":\"/Linux/实用脚本\"},{\"text\":\"软件部署\",\"link\":\"/Linux/软件部署\"}]},{\"text\":\"Nginx\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/Nginx/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/Nginx/进阶篇\"},{\"text\":\"实战篇\",\"link\":\"/Nginx/实战篇\"},{\"text\":\"面试篇\",\"link\":\"/Nginx/面试篇\"}]},{\"text\":\"SSM\",\"collapsed\":true,\"items\":[{\"text\":\"Maven\",\"link\":\"/SSM/Maven\"},{\"text\":\"Spring\",\"link\":\"/SSM/Spring\"},{\"text\":\"SpringMVC\",\"link\":\"/SSM/SpringMVC\"},{\"text\":\"SpringBatch\",\"link\":\"/SSM/SpringBatch\"}]},{\"text\":\"SpringBoot\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/3、SpringBoot/基础篇\"},{\"text\":\"应用篇\",\"link\":\"/3、SpringBoot/应用篇\"},{\"text\":\"新特性\",\"link\":\"/3、SpringBoot/新特性\"},{\"text\":\"运维&原理\",\"link\":\"/3、SpringBoot/运维&原理\"}]},{\"text\":\"SpringCloud\",\"collapsed\":true,\"items\":[{\"text\":\"SpringCloud\",\"link\":\"/4、微服务/进阶\"},{\"text\":\"Sentinel\",\"link\":\"/4、微服务/必备/Sentinel\"}]},{\"text\":\"SpringSecurity\",\"collapsed\":true,\"items\":[{\"text\":\"SpringSecurity基础篇\",\"link\":\"/4、微服务/SpringSecurity/基础篇\"},{\"text\":\"SpringSecurity进阶篇\",\"link\":\"/4、微服务/SpringSecurity/进阶篇\"},{\"text\":\"SpringSecurity高级篇\",\"link\":\"/4、微服务/SpringSecurity/高级篇\"}]},{\"text\":\"Mybatis & MybatisPlus\",\"collapsed\":true,\"items\":[{\"text\":\"Mybatis\",\"link\":\"/Mybatis&MybatisPlus/Mybatis\"},{\"text\":\"MybatisPlus\",\"link\":\"/Mybatis&MybatisPlus/MybatisPlus\"},{\"text\":\"JPA\",\"link\":\"/Mybatis&MybatisPlus/JPA\"}]},{\"text\":\"Git & ChatGPT\",\"collapsed\":true,\"items\":[{\"text\":\"Git\",\"link\":\"/5、运维/Git\"},{\"text\":\"Github\",\"link\":\"/5、运维/Github\"},{\"text\":\"ChatGPT\",\"link\":\"/5、运维/ChatGPT\"},{\"text\":\"Jenkins\",\"link\":\"/5、运维/Jenkins\"},{\"text\":\"Netty\",\"link\":\"/5、运维/Netty\"}]},{\"text\":\"数据库\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL\",\"collapsed\":true,\"items\":[{\"text\":\"MySQL基础\",\"link\":\"/2、数据库/MySQL/MySQL核心/基础\"},{\"text\":\"MySQL进阶\",\"link\":\"/2、数据库/MySQL/MySQL核心/进阶\"},{\"text\":\"MySQL优化\",\"link\":\"/2、数据库/MySQL/MySQL核心/优化\"},{\"text\":\"MySQL设计\",\"link\":\"/2、数据库/MySQL/MySQL核心/设计\"},{\"text\":\"MySQL运维\",\"link\":\"/2、数据库/MySQL/MySQL核心/运维\"},{\"text\":\"分库分表\",\"link\":\"/2、数据库/MySQL/分库分表\"}]},{\"text\":\"Redis\",\"collapsed\":true,\"items\":[{\"text\":\"Redis基础\",\"link\":\"/2、数据库/Redis/Redis基础\"},{\"text\":\"Redis优化\",\"link\":\"/2、数据库/Redis/Redis优化\"},{\"text\":\"Redis原理\",\"link\":\"/2、数据库/Redis/Redis原理\"},{\"text\":\"Redis高级\",\"link\":\"/2、数据库/Redis/Redis高级\"},{\"text\":\"Redis实战\",\"link\":\"/2、数据库/Redis/Redis实战\"},{\"text\":\"本地缓存\",\"link\":\"/2、数据库/Redis/本地缓存\"}]},{\"text\":\"MongoDB\",\"collapsed\":true,\"items\":[{\"text\":\"MongoDB基础\",\"link\":\"/2、数据库/MongoDB/基础\"},{\"text\":\"MongoDB进阶\",\"link\":\"/2、数据库/MongoDB/整合\"}]},{\"text\":\"ElasticSearch\",\"collapsed\":true,\"items\":[{\"text\":\"ES基础\",\"link\":\"/2、数据库/ElasticSearch/1、ES基础\"},{\"text\":\"ES高级\",\"link\":\"/2、数据库/ElasticSearch/3、ES高级\"}]},{\"text\":\"InfluxDB\",\"link\":\"/2、数据库/influxdb\"},{\"text\":\"Neo4j\",\"link\":\"/2、数据库/Neo4j\"}]},{\"text\":\"高并发 & 秒杀 & 分布式\",\"collapsed\":true,\"items\":[{\"text\":\"分布式理论\",\"link\":\"/三高/分布式\"},{\"text\":\"分布式锁\",\"link\":\"/4、微服务/必备/分布式锁\"},{\"text\":\"秒杀\",\"link\":\"/三高/秒杀\"},{\"text\":\"高可用\",\"link\":\"/三高/高可用\"},{\"text\":\"高并发\",\"link\":\"/三高/高并发\"}]},{\"text\":\"云原生\",\"collapsed\":true,\"items\":[{\"text\":\"Docker\",\"link\":\"/云原生/Docker\"},{\"text\":\"K8S\",\"link\":\"/云原生/K8S\"}]},{\"text\":\"可视化 & 监控\",\"collapsed\":true,\"items\":[{\"text\":\"监控基础\",\"link\":\"/可视化 & 监控/监控基础\"},{\"text\":\"监控进阶\",\"link\":\"/可视化 & 监控/监控进阶\"},{\"text\":\"可视化大屏\",\"link\":\"/可视化 & 监控/可视化大屏\"},{\"text\":\"Zabbix\",\"link\":\"/可视化 & 监控/Zabbix\"}]},{\"text\":\"学前端\",\"collapsed\":true,\"items\":[{\"text\":\"HTML+CSS\",\"collapsed\":true,\"items\":[{\"text\":\"HTML基础\",\"link\":\"/1、学前端/1、HTML+CSS/HTML基础\"},{\"text\":\"CSS基础\",\"link\":\"/1、学前端/1、HTML+CSS/CSS基础\"},{\"text\":\"网页进阶\",\"link\":\"/1、学前端/1、HTML+CSS/网页进阶\"}]},{\"text\":\"JS+TS\",\"collapsed\":true,\"items\":[{\"text\":\"JS基础\",\"link\":\"/1、学前端/2、JS+TS/JS 基础\"},{\"text\":\"JS进阶\",\"link\":\"/1、学前端/2、JS+TS/JS 进阶\"},{\"text\":\"ES6基础\",\"link\":\"/1、学前端/2、JS+TS/ES6 基础\"},{\"text\":\"ES6进阶\",\"link\":\"/1、学前端/2、JS+TS/ES6 进阶\"},{\"text\":\"TS基础\",\"link\":\"/1、学前端/2、JS+TS/TypeScript\"}]},{\"text\":\"NodeJS\",\"collapsed\":true,\"items\":[{\"text\":\"Node基础\",\"link\":\"/1、学前端/4、Node/基础篇\"},{\"text\":\"Node进阶\",\"link\":\"/1、学前端/4、Node/进阶篇\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/4、Node/项目实战\"}]},{\"text\":\"Vue\",\"collapsed\":true,\"items\":[{\"text\":\"Vue3进阶\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3进阶\"},{\"text\":\"Vue3高级\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3高级\"},{\"text\":\"Vue3新语法\",\"link\":\"/1、学前端/3、Vue/Vue3/Vue3新语法\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/3、Vue/Vue2/Vue2项目\"}]},{\"text\":\"小程序\",\"collapsed\":true,\"items\":[{\"text\":\"小程序基础\",\"link\":\"/1、学前端/5、小程序/微信小程序\"},{\"text\":\"小程序优化\",\"link\":\"/1、学前端/5、小程序/小程序优化\"},{\"text\":\"uniapp\",\"link\":\"/1、学前端/5、小程序/uniapp\"},{\"text\":\"项目实战\",\"link\":\"/1、学前端/5、小程序/小程序项目\"}]}]},{\"text\":\"计算机基础\",\"collapsed\":true,\"items\":[{\"text\":\"数据结构\",\"link\":\"/计算机基础/数据结构/基础篇\"},{\"text\":\"操作系统\",\"link\":\"/计算机基础/计算机基础/操作系统\"},{\"text\":\"设计模式\",\"link\":\"/计算机基础/设计模式/基础篇\"},{\"text\":\"计算机网络\",\"link\":\"/计算机基础/计算机网络/网络基础\"},{\"text\":\"UML\",\"link\":\"/计算机基础/设计模式/UML\"},{\"text\":\"LeetCode\",\"link\":\"/计算机基础/算法/LeetCode\"}]},{\"text\":\"项目实战\",\"collapsed\":true,\"items\":[{\"text\":\"云尚办公\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/云尚办公/基础篇\"}]},{\"text\":\"小兔鲜\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/小兔鲜/基础篇\"},{\"text\":\"进阶篇1\",\"link\":\"/项目实战/小兔鲜/进阶篇1\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/小兔鲜/进阶篇2\"}]},{\"text\":\"地图\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/百度地图/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/百度地图/进阶篇\"}]},{\"text\":\"苍穹外卖\",\"collapsed\":true,\"items\":[{\"text\":\"进阶篇\",\"link\":\"/项目实战/苍穹外卖/进阶篇\"}]},{\"text\":\"黑马头条\",\"collapsed\":true,\"items\":[{\"text\":\"基础篇\",\"link\":\"/项目实战/黑马头条/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/项目实战/黑马头条/进阶篇\"},{\"text\":\"进阶篇2\",\"link\":\"/项目实战/黑马头条/进阶篇2\"},{\"text\":\"高级篇\",\"link\":\"/项目实战/黑马头条/高级篇\"}]},{\"text\":\"支付\",\"link\":\"/项目实战/支付\"},{\"text\":\"项目推荐\",\"link\":\"/项目实战/项目推荐\"}]},{\"text\":\"团队成员\",\"link\":\"/team\"}],\"siteTitle\":\"任硕的文档\",\"logo\":\"/Vue.png\",\"nav\":[{\"text\":\"Java学前端\",\"items\":[{\"items\":[{\"text\":\"HTML+JS\",\"link\":\"/Java学前端/HTML+JS\"},{\"text\":\"CSS\",\"link\":\"/Java学前端/CSS\"},{\"text\":\"Vue2+组件\",\"link\":\"/Java学前端/Vue2+组件\"},{\"text\":\"Vue3+组件\",\"link\":\"/Java学前端/Vue3+组件\"},{\"text\":\"React\",\"link\":\"/Java学前端/React\"}]}],\"activeMatch\":\"/Java/\"},{\"text\":\"软件测试\",\"items\":[{\"items\":[{\"text\":\"测试基础\",\"link\":\"/软件测试/测试基础\"},{\"text\":\"压力测试\",\"link\":\"/软件测试/压力测试\"}]}]},{\"text\":\"多线程\",\"items\":[{\"items\":[{\"text\":\"基础篇\",\"link\":\"/并发 & 多线程/基础篇\"},{\"text\":\"进阶篇\",\"link\":\"/并发 & 多线程/并发完善\"}]}]},{\"text\":\"开发工具\",\"items\":[{\"items\":[{\"text\":\"Chrome\",\"link\":\"/IDEA/Chrome\"},{\"text\":\"IDEA基础\",\"link\":\"/IDEA/IDEA基础\"},{\"text\":\"IDEA插件\",\"link\":\"/IDEA/IDEA插件\"},{\"text\":\"VS Code\",\"link\":\"/IDEA/VS Code\"}]}]},{\"text\":\"消息中间件\",\"items\":[{\"items\":[{\"text\":\"RabbitMQ\",\"link\":\"/消息中间件/RabbitMQ\"},{\"text\":\"RocketMQ\",\"link\":\"/消息中间件/RocketMQ\"},{\"text\":\"Kafka\",\"link\":\"/消息中间件/Kafka\"},{\"text\":\"Canal\",\"link\":\"/消息中间件/Canal\"}]}]}],\"socialLinks\":[{\"icon\":\"github\",\"link\":\"https://github.com/renshuo123/renshuo123.github.io\"},{\"icon\":\"twitter\",\"link\":\"#\"},{\"icon\":{\"svg\":\"<svg t=\\\"1676028692954\\\" class=\\\"icon\\\" ...</path></svg>\"},\"link\":\"https://github.com/\"}]},\"locales\":{},\"scrollOffset\":90,\"cleanUrls\":false}");</script>
    
  </body>
</html>